2014-04-03 50 views

回答

0

我要去推斷和猜測,你正在尋找一個Counter

>>> from collections import Counter 
>>> x = ["one", "two", "three" ] 
>>> Counter(x) 
Counter({'three': 1, 'two': 1, 'one': 1}) 
1
print dict.fromkeys(["one","two","three"],1) 

是我會怎麼做?如果你真的只想做一個字典從列表(可能加速搜索)

,如果你不關心的值是你可以做

print dict.fromkeys(["one","two","three"]) 
01什麼

,這將是默認None

這對蟒蛇< 2.7工作的好處,再加上它很容易告訴你在做什麼,那裏的字典推導總讓我想起一套內涵的