0

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

首先一次获取用户输入。

输入数字 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)

任何回应将不胜感激..谢谢..

4

1 回答 1

0

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

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)

通过您给定的输入,我得到了预期的输出。

于 2012-08-31T07:28:55.597 回答