使用字典计算列表中由空格分隔的数字的出现次数

时间:2013-11-14 23:24:30

标签: python dictionary counter

如果我有一个数字列表(例如: - 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 个答案:

答案 0 :(得分:2)

您已经关闭了 - 您想要print(count),而不是print(number),以便您打印字典。

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

>>> import collections
>>> numbers = input("Enter numbers ").split(' ')
>>> count = Counter(numbers)
>>> print(count)

答案 1 :(得分: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))

答案 2 :(得分: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

答案 3 :(得分: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")