在python中以行格式显示整数

时间:2012-09-01 19:03:43

标签: python

我试图找出一种方法来让这些整数在行中显示,同时保持其顺序。

但是当我在这里运行代码时,我得到了显示。

Enter numbers 1... 22,34,35,40,55
Enter numbers 2... 12,14,34,47,49
Enter numbers 3... 1,4,10,19,30

Section 1             #
The number seqeuce is 0 between 0 and 9
The number seqeuce is 0 between 10 and 19
The number seqeuce is 1 between 20 and 29
The number seqeuce is 2 between 30 and 39
The number seqeuce is 1 between 40 and 49
The number seqeuce is 1 between 50 and 59
Section 2
The number seqeuce is 0 between 0 and 9
The number seqeuce is 2 between 10 and 19
The number seqeuce is 0 between 20 and 29
The number seqeuce is 1 between 30 and 39
The number seqeuce is 2 between 40 and 49
The number seqeuce is 0 between 50 and 59
Section 3
The number seqeuce is 2 between 0 and 9
The number seqeuce is 2 between 10 and 19
The number seqeuce is 0 between 20 and 29
The number seqeuce is 1 between 30 and 39
The number seqeuce is 0 between 40 and 49
The number seqeuce is 0 between 50 and 59
                      #

这是我想按顺序看到的 对应于范围整数。

Enter numbers 1... 22,34,35,40,55
Enter numbers 2... 12,14,34,47,49
Enter numbers 3... 1,4,10,19,30
Section 1
The number seqeuce is 0 0 1 2 1 1 #
Section 2                         #
The number seqeuce is 0 2 0 1 2 0 # <-- Here is the sequennce from above
Section 3                         #
The number seqeuce is 2 2 0 1 0 0 #

到目前为止我所拥有的。

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 "Section {}".format(i+1) 
    group_counter = collections.Counter(the_list) 
    bin_range = range (6)  
    for bin_tens in bin_range:
        print "The number seqeuce is {} between {} and {}".format(group_counter[bin_tens], bin_tens*10, bin_tens*10+9)

1 个答案:

答案 0 :(得分:1)

简单的重新安排:

print "The number sequence is",
for bin_tens in bin_range:
    print group_counter[bin_tens],
print