多个用户输入打印特定样式

时间:2012-08-31 05:59:05

标签: python

我现在只想输入多个整数组来单独计算。 它会像这样流动。

首先获取所有用户输入。

输入数字1 ... 2,4,23,45,57

输入数字2 ... 9,23,45,47,33

输入数字3 ... 2,41,23,45,55

然后一下子打印出来。

数字序列1 ... 2,0,1,0,1,1

数字序列2 ... 1,0,1,1,2,0

数字序列3 ... 1,0,1,0,2,1

这是我的代码。

import collections 

the_input1 = raw_input("Enter numbers 1... ")
the_input2 = raw_input("Enter numbers 2... ")
the_input3 = raw_input("Enter numbers 3... ")

the_list1 = [int(x) for x in the_input1.strip("[]").split(",")]
the_list2 = [int(x) for x in the_input2.strip("[]").split(",")]
the_list3 = [int(x) for x in the_input3.strip("[]").split(",")]

group_counter = collections.Counter(x//10 for x in the_list1)
group_counter = collections.Counter(x//10 for x in the_list2)
group_counter = collections.Counter(x//10 for x in the_list3)

bin_range = range (6) 

for bin_tens in bin_range: 
    print "There were {} in {} to {}".format(group_counter[bin_tens], bin_tens*10, bin_tens*10+9)

非常感谢任何回复..谢谢..

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望3个输入的数字频率分别在0-9,10-19,...,50-59范围内。我重构了你的程序来实现这个目标:

import collections 

the_inputs = []
for i in range(3):
    the_inputs.append(raw_input("Enter numbers {}... ".format(i+1)))

the_lists = []
for the_input in the_inputs:
    the_lists.append([int(x)//10 for x in the_input.strip("[]").split(",")])

for i, the_list in enumerate(the_lists):
    print "Input {}".format(i+1)
    group_counter = collections.Counter(the_list)
    bin_range = range (6) 
    for bin_tens in bin_range: 
        print "There were {} in {} to {}".format(group_counter[bin_tens], bin_tens*10, bin_tens*10+9)

我得到了你给定输入的预期输出。