我有一个类似这样的词:
ip = { "1" : ['a','b'],
"2" : ['a','c'],
"3" : ['a','b','c','d'],
"4" : ['a','b','d','e']}
我需要找到值集中的哪些项目具有针对它们的最大键数,并且还要按降序列出项目。输出将类似于:
op = {"a":4,"b":3,"c":2,"d":2,"e":1}
但我在某处读到dict不能排序,所以输出也可以是一个元组:
op = [('a', 4), ('b', 3), ('c', 2), ('d', 2), ('e', 1)]
我们可以遍历dict,并且对于值集中的每个项目,会为该项目的defaultdict
增加结果。
op = defaultdict(int)
for k,v in ip.iteritems():
for item in v:
op[item]+=1
op = sorted(op.items(), key=lambda x: x[1], reverse=True)
有没有比嵌套for?
更快/更好的方法答案 0 :(得分:3)
只需使用Counter
和chain.from_iterable
In [9]: from collections import Counter
In [10]: from itertools import chain
In [11]: ip = { "1" : ['a','b'],
...: "2" : ['a','c'],
...: "3" : ['a','b','c','d'],
...: "4" : ['a','b','d','e']}
In [12]: Counter(chain.from_iterable(ip.values()))
Out[12]: Counter({'a': 4, 'b': 3, 'c': 2, 'd': 2, 'e': 1})
要删除重复值,您可以执行以下操作:
>>> from operator import itemgetter
>>> sorted(Counter(chain.from_iterable(map(set, ip.values()))).items(), key=itemgetter(1), reverse=True)
[('a', 4), ('b', 3), ('c', 2), ('d', 2), ('e', 1)]
答案 1 :(得分:0)
这不正确:
sorted(op.items(), key=lambda x: x[1], reverse=True)
尝试改为:
sorted(ip, key=lambda elementInDict: len(ip[elementInDict]), reverse=True)
示例:强>
for elementInDict in sorted(ip, key=lambda elementInDict: len(ip[elementInDict]), reverse=True):
print elementInDict,