def print_most_numbers_occurrences(numbers_str):
number_list = numbers_str.split()
for i in number_list:
i=max(number_list,key=number_list.count)
print(i)
def test_print_most_numbers_occurrences():
print_most_numbers_occurrences('2 3 40 1 5 4 3 3 9 9')
print_most_numbers_occurrences('9 30 3 9 3 1 4')
print_most_numbers_occurrences('19 30 13 4 9 3 1 4')
def main():
test_print_most_numbers_occurrences()
main()
输出:
3
9
4
我想要获取
'9 30 3 9 3 1 4'
中所有最常重复的数字:9和3出现了两次,所以应该报告两次出现的情况而不仅仅是9
。输出结果如下:
3
9
3
4
collections.Counter
,它会让事情变得更好。 - Alex Hall