'Common elements between two differents lists (a list and a list of lists of dictionaries) with no duplicates updated
i need help to get common elements between two lists. One list is a list of strings, the other one is a list of list of dictionaries. I asked this question already here but this is an updated version with dictionaries of dictionary:
Question
For those who don't want to read i have this two list
Mylist1 =
[
[{"ABC":"POS"},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"POS"},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"BREV"},{"ABC":"XUA"},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Magic"},{"ELEMENT":"Question"},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Romance"},{"ELEMENT":"Noun"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Magic"},{"ELEMENT":"Question"},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Romance"},{"ELEMENT":"Noun"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}]
]
Mylist2 =
["XUA","Salmon","Bride","Brother","House"]
and i wanted to get the index that corresponds to the longest common element in this example it's the third element in the list:
[{"ABC":"BREV"},{"ABC":"XUA"},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}]
The best solution I thought was this code:
MySet2 = set(Mylist2)
# `a` is an element of Mylist1, i.e. a list of dictionaries
# `b` is an element of `a`, i.e. a dictionary with a single key-value pair
# `b.values()` contains that value, and `list(b.values()[0])` is the value itself
# `set(list(b.values())[0] for b in a)` is the set of all such values in `a`
# `... & MySet2` keeps only the elements of the set that are also present in MySet2
counts = [len(set(list(b.values())[0] for b in a) & MySet2) for a in Mylist1]
print(counts)
# Now just find the max count
best = 0
for i in range(len(Mylist1)):
if counts[i] > counts[best]:
best = i
print(Mylist1[best])
which works perfectly. But i have a problem what if i have nested dictionaries inside the list? How can i get the same result if there are nested dictionaries. Similar example but with nested dictionaries:
Mylist1 =
[
[{"ABC":{"EL":"MUSTANG"}},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Tea"}],
[{"ABC":"POS"},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"BREV"},{"ABC":{"EL":"XUA"}},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Magic"},{"ELEMENT":"Question"},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Romance"},{"ELEMENT":"Noun"},{"ABC":{"EL":"Arkansas"}},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":"NUON"},{"ELEMENT":"Magic"},{"ELEMENT":"Question"},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}],
[{"ABC":{"EL":"Hair"}},{"ELEMENT":"Romance"},{"ELEMENT":"Noun"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}]
]
Mylist2 =
["MUSTANG","Salmon","Bride","Tea","House"]
what i should get is the 1st line of the list because if we compare the elements in Mylist1 and Mylist2 the line that corresponds with the most common element is the first one:
[{"ABC":{"EL":"MUSTANG"}},{"ELEMENT":"Hair"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Tea"}],
Can somebody help me please, this seems too complicated for me.
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
