Identification of values ​​having the maximum number of keys

I have a dict that looks something like this:

ip = { "1" : ['a','b'], "2" : ['a','c'], "3" : ['a','b','c','d'], "4" : ['a','b','d','e']} 

I need to find which of the elements in the sets of values ​​have the maximum number of keys against them, as well as the elements listed in descending order. The result will be something like this:

 op = {"a":4,"b":3,"c":2,"d":2,"e":1} 

But I read somewhere that a dict cannot be sorted, so the output can also be a tuple:

 op = [('a', 4), ('b', 3), ('c', 2), ('d', 2), ('e', 1)] 

We can iterate through the dict and for each of the elements in the given value, the result is incremented in defaultdict for this element.

 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) 

Is there a faster / better way to do this than a nested one?

+5
source share
2 answers

Just use Counter and 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}) 

To remove a duplicate value, you can always do something like this:

 >>> 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)] 
+3
source

This is not true:

 sorted(op.items(), key=lambda x: x[1], reverse=True) 

Try instead:

 sorted(ip, key=lambda elementInDict: len(ip[elementInDict]), reverse=True) 

Example:

 for elementInDict in sorted(ip, key=lambda elementInDict: len(ip[elementInDict]), reverse=True): print elementInDict, 
0
source

Source: https://habr.com/ru/post/1263167/


All Articles