mydict = {
'name' : ['cat','dog'],
'place' : ['earth','mars'],
'name1' : ['cat','dog'],
'name2' : ['cat','dog'],
'place1' : ['earth','mars'],
'colour' : ['blue','green'],
'colour1' : ['orange']
}
expected_result
[['name','name1','name2'],['place','place1']]
Since 'colour'
and 'colour1'
are not same we will ignore them.
CodePudding user response:
Probably not the fastest solution, but you can find the duplicate values first, then list the corresponding keys:
>>> values = list(mydict.values())
>>> duplicates = set(tuple(v) for v in values if values.count(v) > 1)
>>> [[k for k, v in mydict.items() if tuple(v) == d] for d in duplicates]
[['name', 'name1', 'name2'], ['place', 'place1']]
CodePudding user response:
from collections import defaultdict
rev = defaultdict(list)
for i in mydict:
rev[tuple(mydict[i])].append(i)
answer = [i for i in rev.values() if len(i)>=2]
#[['name', 'name1', 'name2'], ['place', 'place1']]