Python 如何获取列表中每个项目的数量?

Python 如何获取列表中每个项目的数量?,python,Python,我想知道如何得到一个字符串中每个项目的数量。例如: {"harley": ["apple", "apple", "banana"]} 那么我该如何得到这个: Harley has Apple x 2 and Banana x 1 看一看列表也有一个count方法,但是如果你想计算所有的东西,它的效率要低得多。看起来像是集合。计数器在这方面很好。看看列表也有一个count方法,但是如果你想计算所有的东西,它的效率要低得多。Hmmm。看起来像是收藏品。这个柜台很好用。 from collecti

我想知道如何得到一个字符串中每个项目的数量。例如:

{"harley": ["apple", "apple", "banana"]}
那么我该如何得到这个:

Harley has Apple x 2 and Banana x 1

看一看列表也有一个
count
方法,但是如果你想计算所有的东西,它的效率要低得多。看起来像是集合。计数器在这方面很好。看看列表也有一个
count
方法,但是如果你想计算所有的东西,它的效率要低得多。Hmmm。看起来像是收藏品。这个柜台很好用。
from collections import Counter

d = {"harley": ["apple", "apple", "banana"]}
for k,v in d.items():
    print("%s has %s" %(k, ', '.join("%s x %s"%(k,v) for k,v in Counter(v).items())))
d = {"harley": ["apple", "apple", "banana"]}

from collections import Counter
for k,v in d.iteritems():
    print k + ' has ' + ' and '.join('{0} x {1}'.format(name, count) for name, count in Counter(v).iteritems())