Reputation: 7924
I have the following script:
dict_class = dict()
for p in points:
dict_class[p.class] = dict_class.get(p.class, 0) + 1
print dict_class
{1: 1314, 2: 1050}
where "points" is a list of value. My question is, is it possible to convert in a list comprehension for dictionaries in order to speed up my code?
Upvotes: 2
Views: 110
Reputation: 113940
from collections import Counter
from operator import attrgetter
Counter(map(attrgetter("class"),points))
Upvotes: 6