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