在阅读了这个问题的答案How to count the frequency of the elements in a list?后,我想知道如何计算某些事物的频率,同时通过像索引这样的东西来检索一些额外的信息。例如
a = ['fruit','Item#001']
b = ['fruit','Item#002']
c = ['meat','Item#003']
foods = [a,b,c]
现在我想计算一下食物中水果的数量。如果我在每个数组a,b和c的第一个索引上使用counter,结果将具有水果数,但我无法访问它是哪个项目。从本质上讲,如果我使用most_common
,我会得到水果出现的次数列表,例如('水果',2),如何从这两个中获取所有项目发生。
我想避免使用像这个问题Python how to use Counter on objects according to attributes
这样的属性示例,它将执行我想要的操作,而不一定是Counter方法实际执行的操作。
counts = Counter(foods)
counts.most_common(10)
print counts
-> (('fruit',2)('meat',1))
print counts[0].Something_Like_Expand_Method()
-> ['fruit','Item#001'],['fruit','Item#002']
答案 0 :(得分:4)
要计算一个值出现的频率,同时您想要选择这些值,您只需选择这些值并计算您选择的数量:
fruits = [f for f in foods if f[0] == 'fruit']
fruit_count = len(fruits)
如果您需要对所有参赛作品执行此操作,您确实希望使用字典分组您的项目:
food_groups = {}
for food in foods:
food_groups.setdefault(food[0], []).append(food[1])
此时你可以要求任何一组,加上他们的长度:
fruit = food_groups['fruit']
fruit_count = len(fruit)
如果您仍然需要知道哪个食物组最常见,那么您可以使用max()
功能:
most_common_food = max(food_groups, key=lambda f: len(food_groups[f])) # just the food group name
most_common_food_items = max(food_groups.values(), key=len) # just the food group name
或者您可以通过将字典映射键传递给值长度来从组中创建Counter
:
group_counts = Counter({f: len(items) for f, items in food_groups.iteritems()})
for food, count in group_counts.most_common(2):
print '{} ({}):'.format(food, count)
print ' items {}\n'.format(', '.join(food_groups[food]))
演示:
>>> from collections import Counter
>>> a = ['fruit','Item#001']
>>> b = ['fruit','Item#002']
>>> c = ['meat','Item#003']
>>> foods = [a,b,c]
>>> food_groups = {}
>>> for food in foods:
... food_groups.setdefault(food[0], []).append(food[1])
...
>>> food_groups
{'fruit': ['Item#001', 'Item#002'], 'meat': ['Item#003']}
>>> group_counts = Counter({f: len(items) for f, items in food_groups.iteritems()})
>>> for food, count in group_counts.most_common(2):
... print '{} ({}):'.format(food, count)
... print ' items {}\n'.format(', '.join(food_groups[food]))
...
fruit (2):
items Item#001, Item#002
meat (1):
items Item#003