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. E.g. :
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"]
How can i check if the strings in Mylist2 are contained in MyList1's value and get the index that corresponds to the longest common element? E.g: In my example with Mylist1 and Mylist2 i want to get the 3nd element:
[{"ABC":"BREV"},{"ABC":"XUA"},{"ELEMENT":"Salmon"},{"ELEMENT":"Bride"},{"ELEMENT":"Brother"},{"ELEMENT":"House"},{"ELEMENT":"Fish"}]
How should i procede to do it? Can someone give me directions on how to do it?
Edit: The two list are generated automatically.
CodePudding user response:
I believe this does the job?
Why is it so difficult?
Mylist1[2]
Output :
[{'ABC': 'BREV'},
{'ABC': 'XUA'},
{'ELEMENT': 'Salmon'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Fish'}]
CodePudding user response:
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"]
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])
CodePudding user response:
What I can comprehend is that you want all dictionaries from Mylist1 whose keys are present in Mylist2, right?
Not sure if you wanted to do this, but here's the code:
final_list_of_dicts = []
for each_list in Mylist1:
for each_dict in each_list:
for key,value in each_dict.items():
# if key in Mylist2 or value in Mylist2:
if value in Mylist2:
final_list_of_dicts.append(each_dict)
final_list_of_dicts
Output :
[{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Salmon'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ABC': 'XUA'},
{'ELEMENT': 'Salmon'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'},
{'ELEMENT': 'Bride'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'House'}]
Also for unique :
from ast import literal_eval
unique_dicts = [literal_eval(each_string) for each_string in list(set([str(each_dict) for each_dict in final_list_of_dicts] ))]
unique_dicts
Out :
[{'ELEMENT': 'House'},
{'ELEMENT': 'Brother'},
{'ELEMENT': 'Salmon'},
{'ABC': 'XUA'},
{'ELEMENT': 'Bride'}]
I believe you can change these to your requirements :) Good Luck Mate! GodSpeed!
