具有键列表的快速字典填充

时间:2014-08-15 20:13:36

标签: python list dictionary

d = {} # or d = defaultdict(int)

list_of_lists = [[9, 7, 5, 3, 1], [2, 1, 3, 2, 5, 3, 7], [3, 5, 8, 1]]

for lst in list_of_lists:
    for key in lst:
        try:
            d[key] += 1
        except:
            d[key] = 1

有没有办法在没有for循环的情况下执行此操作?

3 个答案:

答案 0 :(得分:7)

使用collections.Counter() object和生成器表达式:

from collections import Counter

d = Counter(i for nested in list_of_lists for i in nested)

或用itertools.chain.from_iterable()替换生成器表达式:

from itertools import chain

d = Counter(chain.from_iterable(list_of_lists))

演示:

>>> from collections import Counter
>>> from itertools import chain
>>> list_of_lists = [[9, 7, 5, 3, 1], [2, 1, 3, 2, 5, 3, 7], [3, 5, 8, 1]]
>>> Counter(i for nested in list_of_lists for i in nested)
Counter({3: 4, 1: 3, 5: 3, 2: 2, 7: 2, 8: 1, 9: 1})
>>> Counter(chain.from_iterable(list_of_lists))
Counter({3: 4, 1: 3, 5: 3, 2: 2, 7: 2, 8: 1, 9: 1})

答案 1 :(得分:0)

我的理解是你要计算列表列表中每个整数的频率。

您可以使用numpy.bincount执行此操作。实际计数非常快,因为numpy的核心是C ++。需要完成一些工作才能以字典格式获取数据 - 您可能只使用由此生成的numpy.array。这些代码的大部分内容只是转换为不同的格式,如果您的应用程序允许,您可以将其取消。

list_of_lists = [[9, 7, 5, 3, 1], [2, 1, 3, 2, 5, 3, 7], [3, 5, 8, 1]]

import numpy as np
x = sum(list_of_lists, []) #convert your list of lists to a flat list
y = np.bincount(x) #count frequency of each element


#convert to dict
d = {}
ctr = 0 
while ctr < len(y):
    d[ctr] = y[ctr]
    ctr += 1

答案 2 :(得分:0)

如果您对Counter(the right answer BTW)过敏,可以使用setdefault:

d={}
for key in (e for sl in list_of_lists for e in sl): 
    d[key] = d.setdefault(key,0) + 1
相关问题