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}
其中“points”是一个值列表。 我的问题是,为了加速我的代码,是否可以在列表中理解字典?
答案 0 :(得分:6)
from collections import Counter
from operator import attrgetter
Counter(map(attrgetter("class"),points))