simular this question, want know intersection between 2 lists of dictionaries.
the other questions asks how intersection of dictionaries inside list:
>>> dicts = [dict(a=3, b=89, d=2), dict(a=3, b=89, c=99), dict(a=3, b=42, c=33)] and solution was:
dict(set.intersection(*(set(d.iteritems()) d in dicts))) however know how intersection of 2 list of dictionaries , not intersection of inside dictionaries.
if had:
>>> dicts1 = [dict(a=3, b=89, d=2), dict(a=3, b=89, c=99), dict(a=3, b=42, c=33)] >>> dicts2 = [dict(a=3, b=89, d=2), dict(a=1, b=89, c=99), dict(a=0, b=42, c=33)] i want
{a=3m, b=89, b=42} to print not because happens in of dictionaries in dicts1 because happens in dictionary in dicts1 , dictionary in dict2. dont care in each array. happens.
how (note if have fixed list of keys, don't need first line):
keys = set([y x in dicts1 y in x.keys() ] + [y x in dicts2 y in x.keys() ]) key in keys: valsdicts1 = set([x[key] x in dicts1 if key in x]) valsdicts2 = set([x[key] x in dicts2 if key in x]) print key, list(valsdicts1 & valsdicts2) prints example:
a [3] c [33, 99] b [89, 42] d [2] obviously if don't want print it, else last line.
Comments
Post a Comment