将列表转换为字典

时间:2014-04-28 18:27:07

标签: python dictionary

这是我的列表,我想将其转换为关键值1-9的字典。我使用了benford法则,我试图找到每个数字的起始位数,并记录每个数字是起始数字的次数。这是我的清单。它持续的时间更长,但我只复制了少量。

['2930', '4782', '709', '433', '29861', '20115', '15114', '2417', '737', '966', '8863', '355', '23561', '8272', '1288', '1961', '746', '318', '2100', '1890', '2643', '24194', '10109', '6587', '57828', '877', '488', '624', '722', '322', '222', '8342', '3163',......

1 个答案:

答案 0 :(得分:9)

from collections import Counter
l = ['2930', '4782', '709', '433', '29861', '20115', '15114', '2417', '737', '966', '8863', '355', '23561', '8272', '1288', '1961', '746', '318', '2100', '1890', '2643', '24194', '10109', '6587', '57828', '877', '488', '624', '722', '322', '222', '8342', '3163']
c = Counter(x[0] for x in l)


>>> c
Counter({'2': 9, '1': 5, '3': 4, '7': 4, '8': 4, '4': 3, '6': 2, '5': 1, '9': 1})