appearences鉴于Python的统计项的列表
listEx = ['cat', 'dog', 'cat', 'turtle', 'apple', 'bird', 'bird']
for i in listEx:
if listEx.count(i) > 1:
print "this item appears more than once", i
else:
print "this item appears only once", i
我希望它打印猫和鸟类出现不止一次(或只是生产['cat', 'bird']
)。我怎样才能做到这一点?
我想你的意思'd.get(V,0)'。甚至是'd = collections.defaultdict(int)'。 –
没错。 'd = collections.defaultdict(int)' – MarcoS