0

I have a dictionary which has the following form:

{ 1:['a','b'],
  2:['c','d'],
  3:['b','e'],
  4:['g','c'] 
..........
}

I want to find out if any values from the values matches with values in another key and then add it to another dictionary.In this example, in key 1 value 'b' matches with value 'b' in key 3. Si I want to group these together in a new dictionary for example

{100:['a','b''e']}.

Any suggestion on implementation or possible approaches preferably in Python would be highly appreciated.

Mark
  • 90,562
  • 7
  • 108
  • 148
NitrAl
  • 1

1 Answers1

0

how do you want to add it to a new dictionary the whole key or just the value to a new key? this is what I understood you want

dict = { 1:['a','b'],2:['c','d'],3:['b','e'],4:['g','c']}
matchs = {}
for key in dict:
    for key2 in dict:
        if key == key2:
            continue
        else:
            for value in dict[key]:
                if value in dict[key2]:
                    matchs[key] = key2



print(matchs)

OUTPUT: {1: 3, 2: 4, 3: 1, 4: 2}