Home > Blockchain >  Get keys from a dictionary that contains duplicate list values, then store their corresponding keys
Get keys from a dictionary that contains duplicate list values, then store their corresponding keys

Time:05-26

I've tried to word this the best way that I possibly can, but it will probably be more clear if I provide an example of what I am trying to acheive:

Input:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

Intended output:

[["person1","person2"],["person3","person4","person5"]]

Handling the lists in the dictionary is proving to be quite a challenge.

Any help with this would be greatly appreciated.

Appologies, I forgot to include what I have tried so far. As mentioned above - I am having issues with the lists:

rev_dict = {}
  
for key, value in source_dictionary.items():
    rev_dict.setdefault(value, set()).add(key)
      
result = [key for key, values in rev_dict.items()
                              if len(values) > 1]

CodePudding user response:

Assuming you want to join the keys by identical value, use a defaultdict:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

from collections import defaultdict

d = defaultdict(list)
for key, value in source_dictionary.items():
    d[tuple(value)].append(key)
    
out = list(d.values())

Alternative with setdefault:

d = {}
for key, value in source_dictionary.items():
    d.setdefault(tuple(value), []).append(key)
    
out = list(d.values())

output:

[['person1', 'person2'], ['person3', 'person4', 'person5']]

CodePudding user response:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

L = []
for i in source_dictionary.values():
    K = []
    for j in source_dictionary.keys():
        if source_dictionary[j] == i :
            K.append(j)
    if K not in L:
        L.append(K)

print(L)

CodePudding user response:

For a one liner solution with itertools.groupby:

[[e for e, _ in g] for _, g in groupby(sorted(d.items(), key=lambda x: x[1]), lambda x: x[1])]

Example usage:

>>> from itertools import groupby
>>> source_dictionary = {"person1": ["x1","x2","x3","x4"],
...                      "person2": ["x1","x2","x3","x4"],
...                      "person3": ["x1","x2"],
...                      "person4": ["x1","x2"],
...                      "person5": ["x1","x2"]}
>>> [[e for e, _ in g] for _, g in groupby(sorted(source_dictionary.items(), key=lambda x: x[1]), lambda x: x[1])]
[['person1', 'person2'], ['person3', 'person4', 'person5']]
  • Related