python字典唯一值的计数

时间:2013-05-06 20:02:29

标签: python dictionary

我在Python中计算每个键的不同值时遇到问题。

我有一本词典

[{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]

我需要分别为每个键打印不同值的数量。

这意味着我想要打印

abc 3
xyz 1
pqr 4

请帮忙。

谢谢

6 个答案:

答案 0 :(得分:7)

使用collections.Counter() instance以及一些链接:

from collections import Counter
from itertools import chain

counts = Counter(chain.from_iterable(e.keys() for e in d))

这可确保正确计算输入列表中包含多个键的词典。

演示:

>>> from collections import Counter
>>> from itertools import chain
>>> d = [{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]
>>> Counter(chain.from_iterable(e.keys() for e in d))
Counter({'pqr': 5, 'abc': 3, 'xyz': 1})

或输入词典中的多个键:

>>> d = [{"abc":"movies", 'xyz': 'music', 'pqr': 'music'}, {"abc": "sports", 'pqr': 'movies'}, {"abc": "music", 'pqr': 'sports'}, {"pqr":"news"}, {"pqr":"sports"}]
>>> Counter(chain.from_iterable(e.keys() for e in d))
Counter({'pqr': 5, 'abc': 3, 'xyz': 1})

Counter()还有其他有用的功能,例如.most_common() method以反向排序顺序列出元素及其计数:

for key, count in counts.most_common():
    print '{}: {}'.format(key, count)

# prints
# 5: pqr
# 3: abc
# 1: xyz

答案 1 :(得分:4)

无需使用计数器。你可以用这种方式实现:

# input dictionary
d=[{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]

# fetch keys
b=[j[0] for i in d for j in i.items()]

# print output
for k in list(set(b)):
    print "{0}: {1}".format(k, b.count(k))

答案 2 :(得分:3)

您所描述的内容 - 每个键具有多个值的列表 - 可以通过以下方式更好地显示:

{'abc': ['movies', 'sports', 'music'],
 'xyz': ['music'],
 'pqr': ['music', 'movies', 'sports', 'news']
}

在这种情况下,你需要做更多的工作来插入:

  1. 查找键以查看它是否已存在
    • 如果不存在,请创建值为[](空列表)
    • 的新密钥
  2. 检索值(与密钥关联的列表)
  3. 使用if value in查看列表中是否存在要检查的值
  4. 如果新值不在,.append()
  5. 这也可以轻松计算存储的元素总数:

    # Pseudo-code
    for myKey in myDict.keys():
        print "{0}: {1}".format(myKey, len(myDict[myKey])
    

答案 3 :(得分:2)

>>> d = [{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"},
... {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, 
... {"pqr":"sports"}]
>>> from collections import Counter
>>> counts = Counter(key for dic in d for key in dic.keys())
>>> counts
Counter({'pqr': 5, 'abc': 3, 'xyz': 1})
>>> for key in counts:
...     print (key, counts[key])
...
xyz 1
abc 3
pqr 5

答案 4 :(得分:1)

使用collections.Counter。假设您有一个项目词典的列表...

from collections import Counter
listOfDictionaries = [{'abc':'movies'}, {'abc':'sports'}, {'abc':'music'},
    {'xyz':'music'}, {'pqr':'music'}, {'pqr':'movies'},
    {'pqr':'sports'}, {'pqr':'news'}, {'pqr':'sports'}]
Counter(list(dict)[0] for dict in zzz)

答案 5 :(得分:1)

在@akashdeep解决方案的基础上构建,该解决方案使用该集但是给出错误的结果,因为不计算" distinct"问题中提到的要求(pqr应该是4,而不是5)。

# dictionary
d=[{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]

# merged dictionary
c = {}
for i in d:
    for k,v in i.items():
        try:
            c[k].append(v)
        except KeyError:
            c[k] = [v]

# counting and printing
for k,v in c.items():
    print "{0}: {1}".format(k, len(set(v)))

这将给出正确的:

xyz: 1
abc: 3
pqr: 4