2
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」是值的列表。 我的問題是,爲了加快我的代碼,是否有可能將字典的理解轉換爲字典?用於詞典的Python列表理解

+2

@Bill:尚未與此循環,因爲這需要自引用。 –

回答

6
from collections import Counter 
from operator import attrgetter 
Counter(map(attrgetter("class"),points)) 
+1

這應該加快他的代碼:) –