0

如果我有一个数字列表(例如:-)1 2 3 3 2 2 2 1 3 4 5 3,我如何使用 python 中的字典来计算列表中每个数字的出现次数?所以输出是这样的:

输入以空格分隔的数字:1 2 3 3 2 2 2 1 3 4 5 3

{'1': 2, '3': 4, '2': 4, '5': 1, '4': 1}

1 occurs 2 times

3 occurs 4 times

2 occurs 4 times

5 occurs one time

4 occurs one time 

此外,如果一个数字只出现一次,则输出应该是“一次”。

这是我到目前为止所拥有的:

numbers=input("Enter numbers separated by spaces:-")
count={}
for number in numbers:
    if number in count:
        count[number] = count[number]+1
    else:
        count[number] = 1

print(number)

但是我的输出最终是最后一个数字,我输入有人可以帮助我吗?

好的,这就是我现在所拥有的:

numbers = input("Enter numbers separated by spaces:-") # i.e. '1 2 3 3 2 2 2 1 3 4 5 3'
my_list = list(map(int, numbers.strip().split(' ')))
count = {}
for x in set(my_list):
   count[x] = my_list.count(x)
print(count)
for key, value in count.items():
    if value == 1:
         print('{} occurs one time'.format(key))
    else:
         print('{} occurs {} times'.format(key, value))

这就是我现在所拥有的,看起来还不错,如果有任何方法可以使它变得更好,请告诉我。非常感谢大家

4

4 回答 4

2

你很接近 - 你想要print(count),而不是print(number)你正在打印字典。

顺便说一句,您可以使用库中的Countercollections为您执行此操作:

>>> import collections
>>> numbers = input("Enter numbers ").split(' ')
>>> count = Counter(numbers)
>>> print(count)
于 2013-11-14T23:27:20.783 回答
1

尝试计数器:

>>> import collections
>>> number_string = '1 2 3 3 2 2 2 1 3 4 5 3'
>>> number_list = number_string.split(' ') # provide a list split on the spaces
>>> counts = collections.Counter(number_list)
>>> counts
Counter({'2': 4, '3': 4, '1': 2, '4': 1, '5': 1})

您也可以边走边算:

>>> counts = collections.Counter()
>>> for l in "GATTACA":
...     counts[l] +=1
... 
>>> counts
Counter({'A': 3, 'T': 2, 'C': 1, 'G': 1})

要打印得很好:

import collections

def print_counts_in_spaced_string(number_string):
    number_list = number_string.split(' ') # provide a list split on the spaces
    counts = collections.Counter(number_list)
    for key, value in counts.items():
        format_string = '{key} occurs {value} {times}'
        if value == 1:
            value, times = 'one', 'time'
        else:
            times = 'times'
        print(format_string.format(key=key, value=value, times=times))

number_string = '1 2 3 3 2 2 2 1 3 4 5 3'
print_counts_in_spaced_string(number_string)

哪个打印:

1 occurs 2 times
2 occurs 4 times
3 occurs 4 times
4 occurs one time
5 occurs one time
于 2013-11-14T23:32:53.323 回答
1

set()方法返回一个可迭代对象中唯一值的列表,该方法返回一个列表中count()特定数字的出现次数。

使用这些事实,您可以通过执行以下操作来解决问题。

numbers = input("Enter numbers seperated by spaces:-") # i.e. '1 2 3 3 2 2 2 1 3 4 5 3'
my_list = list(map(int, numbers.strip().split(' ')))
count = {}
for x in set(my_list):
    count[x] = my_list.count(x)

for key, value in count.items():
    if value == 1:
        print('{} occurs one time'.format(key))
    else:
        print('{} occurs {} times'.format(key, value))
于 2013-11-14T23:28:25.797 回答
0

你的问题是你从一个空的dict. 这意味着您的检查if number in count将始终失败(因为count是空字典)。

我建议collections.Counter按照其他人的建议使用。但如果你真的想调试你的代码,那么我会这样做:

numbers=input("Enter numbers seperated by spaces:-")
count={}
for number in numbers.split(): # ignore all the white-spaces
  if number not in count:
    count[number] = 0
  count[number] += 1
for num,freq in count.items():
  print(num, "occurs", freq, "times")
于 2013-11-14T23:44:21.127 回答