查找特定整数的出现次数

时间:2013-11-15 06:44:19

标签: python integer

我一直在第9行得到一个关键词,我不能为我的生活弄清楚。

这是我的代码

numbers = (input("Enter numbers separated by spaces > "))
alist = []
alist = numbers.split()
count = {}
for word in alist:
    if word not in alist:
        (count[word]) = 1
    else:
        (count[word]) = (count[word]) + 1
print(count)
for k,v in count.item():
    if v == 1:
        print(k, "occurs", v, "time")
    else:
        print(k, "occurs", v, "times")

1 个答案:

答案 0 :(得分:1)

word not in alist始终为false,因为for循环遍历alist

替换以下行:

if word not in alist:

if word not in count:

顺便说一句,你不需要围绕count[word]的括号。

相关问题