Home > front end >  Most frequent element in python hashed dictionary
Most frequent element in python hashed dictionary

Time:07-14

I have the following dictionary:

data = {112: [25083], 25091: [6939], 32261: [9299, 6939, 3462], 32934: [7713, 6762, 6939], 34854: [6939], 56630: [7713]}

I am trying to overcome with the most frequent values. The output that I want to have look like:

{6639:4, 7713:2, 25083:1, 9299:1, 3462:1, 6762:1}

or

{6639:[25091, 32261, 32934, 34854], 7713:[32934, 56630], 25083:[25083], 9299:[32261], 3462:[32261], 6762:32934 }

I use the script for the normal dictionary, but for unhashed I dont know how to manage it.

k = {}
from collections import defaultdict
for key, val in data.items():
    for i in val:
        k.setdefault(i, set()).add(k)

Thank you in advance

CodePudding user response:

You can use Counter and defaultdict:

from collections import Counter, defaultdict
from itertools import chain

data = {112: [25083], 25091: [6939], 32261: [9299, 6939, 3462], 32934: [7713, 6762, 6939], 34854: [6939], 56630: [7713]}

counter = Counter(chain.from_iterable(data.values()))

print(counter) # Counter({6939: 4, 7713: 2, 25083: 1, 9299: 1, 3462: 1, 6762: 1})

data_inverted = defaultdict(list)
for k, vs in data.items():
    for v in vs:
        data_inverted[v].append(k)

print(data_inverted)
# defaultdict(<class 'list'>,
# {25083: [112],
#  6939: [25091, 32261, 32934, 34854],
#  9299: [32261],
#  3462: [32261],
#  7713: [32934, 56630],
#  6762: [32934]})

Actually, if you are going to get data_inverted anyway, you can use the following after data_inverted (instead of using collections.Counter:

counter = {k: len(v) for k, v in data_inverted.items()}
  • Related