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

私があなたを正しく理解しているなら、あなたはあなたの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)

私はあなたの与えられた入力で期待される出力を取得します。

于 2012-08-31T07:28:55.597 に答える