0

After reading the answers on this question How to count the frequency of the elements in a list? I was wondering how to count the frequency of something, and at the same time retreive some extra information, through something like an index. For example

a = ['fruit','Item#001']
b = ['fruit','Item#002']
c = ['meat','Item#003']
foods = [a,b,c]

Now I want to count how many times fruit is in the list foods. If I use counter on the first index of each array a,b, and c, the results will have the number of fruit, but I can not access which item it was. In essence, if I use most_common, I will get a list of the number of times fruit appears, like ('fruit',2), how can I get all the items from both of those occurrences.

I would like to avoid using the attributes like this question Python how to use Counter on objects according to attributes

Example, where it would be doing what I would like, not necessarily what the Counter method actually does.

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']
Community
  • 1
  • 1
user-2147482637
  • 2,115
  • 8
  • 35
  • 56

1 Answers1

4

To count how often one value occurs and at the same time you want to select those values, you'd simply select those values and count how many you selected:

fruits = [f for f in foods if f[0] == 'fruit']
fruit_count = len(fruits)

If you need to do this for all your entries, you really want to group your items, using a dictionary:

food_groups = {}
for food in foods:
    food_groups.setdefault(food[0], []).append(food[1])

at which point you can ask for any of the groups, plus their length:

fruit = food_groups['fruit']
fruit_count = len(fruit)

If you still need to know which food group is most common, you can then just use the max() function:

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

or you can create a Counter from the groups by passing in a dictionary mapping key to value length:

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]))

Demo:

>>> 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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343