Reputation: 43
from collections import Counter
# Frequency table for a list of numbers
def frequency_table(numbers):
table=Counter(numbers)
for number in table.most_common():
print('{0} \t {1}'.format(number[0],number[1]))
if __name__=='__main__':
scores=[7,8,9,2,10,9,9,9,9,4,5,6,1,5,6,7,8,6,1,10]
frequency_table(scores)
After I run this, the 2 term turns into twenty one, I try many times, I put the results below
9 5
6 3
7 2
8 2
10 2
5 2
1 2
twenty one
4 1
Upvotes: 0
Views: 42
Reputation: 2718
you have a list of integers and you create counter from it so:
[7,8,9,2,10,9,9,9,9,4,5,6,1,5,6,7,8,6,1,10]
becomes
Counter({7: 2, 8: 2, 9: 5, 2: 1, 10: 2, 4: 1, 5: 2, 6: 3, 1: 2})
which most_common items is a list of tuples which elements are integers:
So, I do not see anything odd in the output.
[(9, 5), (6, 3), (7, 2), (8, 2), (10, 2), (5, 2), (1, 2), (2, 1), (4, 1)]
the most common list has 9 elements which equals to number of key, value pair of the Counter.
in the most common list tuple (2,1)
exists
you can print it as:
for x,y in Counter(scores).most_common():
print(f"{x} {y}")
print output:
9 5
6 3
7 2
8 2
10 2
5 2
1 2
2 1
4 1
Upvotes: 1