Say I have an ordered dict:
import collections
collections.OrderedDict([('great_key', {'keyword': {'blue', 'yellow'}}), ('super_key', {'keyword': {'lock', 'door'}})])
and a list of potential_matches
: [red, red, blue, one]
There are two lists that I need to populate:
correct_key = []
or incorrect_match = []
If the potential match is a keyword of one of the keys in the dict, then its key goes in correct_key
, else the word goes in incorrect_match
.
Result of this example should be:
correct_key = [great_key]
, incorrect_match = [red, red, one]
Here is what I tried:
correct = []
incorrect = []
for word in potential_matches:
for key, value in ordered_dict.items():
if word in value["keyword"] and word not in correct:
correct.append(word)
elif word not in value["keyword"] and word not in correct and word not in incorrect:
incorrect.append(word)
This does not work and does not seem efficient either.
It cannot be a set bc it needs to preserve order, and duplicates are fine, as well as multiple items in the lists.
It shouldn't return at the first sight of a match, since all items of the dict that are a match should be in the final list.
Essentially, all remaining words that are not a match should simply go to the other list.
What is the most efficient (and readable) way to get this done?
Note: I asked similar questions previously although in those cases the situation and answer called for the use of a python sets, as items were unique, or keyword instead of key.