【发布时间】:2021-09-02 04:56:55
【问题描述】:
我正在尝试根据变量的出现次数来做一个条件。 如果任何变量出现不止一次,如果每个变量出现一次,它只会显示为正确,则它会显示为错误。我试过了,但没有用:
from collections import Counter
z = ['blue', 'red', 'yellow']
Counter(z)
print(Counter(z))
if Counter(z).items() != 1:
print('Wrong')
else:
print('Right')
【问题讨论】:
-
1) 每次调用 Counter(z) 时,都会创建一个新的 Counter 对象。 2) .items() 没有做你认为的事情(查找 Counter 和 python dicts 的文档)
标签: python collections counter