如何计算几个词典中重复键的数量?

时间:2016-01-15 19:18:11

标签: python dictionary count key

我们说我有大量的词典(可能是10到39种词典)。我想计算所有词典中每个键的数量。即如果我有3个词典:

  • {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'}
  • {1: 'url1', 7: 'url3'}
  • {5: 'url4', 10: 'url5'}

然后在结果中我应该得到{1: [2, 'url1'], 10: [1, 'url5'], 3: [1, 'url2'], 5: [2, 'url4'], 7: [2, 'url3']}

我来到以下代码:

lists = [{1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'}, {1: 'url1', 7: 'url3'}, {5: 'url4', 10: 'url5'}]
result = {}
for l in lists:
    for i in l:
        if i in result:
            result[i][0] += 1
        else:
            result[i] = [1, l[i]]

有更好(更快)的方法吗?

1 个答案:

答案 0 :(得分:2)

如果您可以接受稍微不同的输出,这可能对您有用:

from collections import Counter

dicts = [
    {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'},
    {1: 'url1', 7: 'url3'},
    {5: 'url4', 10: 'url5'},
]

result = Counter()
for d in dicts:
    result.update(d.keys())

print dict(result)

请注意,它有键和计数,但没有值。

可替换地:

from collections import Counter
from itertools import chain

dicts = [
    {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'},
    {1: 'url1', 7: 'url3'},
    {5: 'url4', 10: 'url5'},
]

result = Counter(chain.from_iterable(dicts))

print dict(result)

最终版本:此版本完全生成您要求的输出:

from collections import Counter
from itertools import chain

dicts = [
    {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'},
    {1: 'url1', 7: 'url3'},
    {5: 'url4', 10: 'url5'},
]

result = Counter(chain.from_iterable(d.items() for d in dicts))
result = {k:[n,v] for ((k,v),n) in result.items()}

print dict(result)