0

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.

Jay Jung
  • 1,805
  • 3
  • 23
  • 46
  • "If the potential match is a keyword of one of the keys in the dict" is confusing... – T.Woody Aug 27 '18 at 04:00
  • @T.Woody i agree. But I believe it's pretty accurate to what I'm trying to express. If item in `potential_match` happens to be a value in the `keyword` of a key in the dict... Would that be better? – Jay Jung Aug 27 '18 at 04:02
  • I think the example answer does a decent job of illustrating the expected outcome – Jay Jung Aug 27 '18 at 04:03
  • I have tested some stuff, and it might be what you are looking for but I want to make some stuff clear first. 'duplicates are fine, as well as multiple items in the lists.' So does this mean that the code can have duplicate matches in the incorrect_match list? Such as: ['red', 'one', 'red', 'one'] – Joseph Seung Jae Dollar Aug 27 '18 at 04:07
  • @JosephSeungJaeDollar those conditions are simply reiterating common features of a python list. wanted to be clear that duplicates are okay, as well as iterating through the whole oDict as to not simply return on the first sight of a match. – Jay Jung Aug 27 '18 at 04:08
  • @JosephSeungJaeDollar if the `potential_match` given happened to have `[red, red, one, two]`, then two `red`'s are acceptable in the `incorrect_match` – Jay Jung Aug 27 '18 at 04:10

1 Answers1

0

Based off of running your code, I believe there to be no matches.

>>> for key in f:
...   print(f[key])
... 
{'keywords': {'blue', 'yellow'}}
{'keywords': {'door', 'lock'}}

And then:

>>> for key in f:
...  v = f[key]
...  for b in v:
...   print(v[b])
... 
{'blue', 'yellow'}
{'door', 'lock'}
>>> for key in f:
...  for b in v:
...   for c in v[b]:
...    print(c)
... 
door
lock
door
lock

The issue is that you are using keywords twice. Change keywords in the second iterations to be keywords2.

T.Woody
  • 1,142
  • 2
  • 11
  • 25
  • @Casper It is, because for each element in `f` you compare that to each element in `potential_matches`. That is `n` elements in `f` and `n` elements in `potential_matches`. – T.Woody Aug 27 '18 at 04:19
  • Your answer does not sort the matched key into a list and the non-matches into another list. – Jay Jung Aug 27 '18 at 04:22
  • @Casper I can write the code specific for you... But I feel like I pointed out the major issue with using `keywords` twice, pretty accurately. – T.Woody Aug 27 '18 at 04:24
  • not sure I follow. `keyword` is the actual name of the key in that particular dictionary. It cannot be changed – Jay Jung Aug 27 '18 at 04:26
  • @Casper If you need further help, I would be glad to assist via email chat: Tanner.L.Woody@gmail.com – T.Woody Aug 27 '18 at 04:26
  • @Casper [see here how a dictionary cannot have two similar keys](https://stackoverflow.com/questions/10664856/make-dictionary-with-duplicate-keys-in-python) – T.Woody Aug 27 '18 at 04:29
  • That is correct. However the keys I am trying to extract are the outer keys which are `great_key` or `super_key`. The inner keys can be the same Perhaps I am using the term `key` incorrectly. But It is an `OrderedDict` – Jay Jung Aug 27 '18 at 04:31
  • @Casper what mandates that the keys have to both be `keywords`? Also, a `dictionary` is composed of `key` and `value` pairs. Those are the specific terms. – T.Woody Aug 27 '18 at 04:34
  • @Casper again, feel free to contact me via email chat. That would be much easier to discuss. – T.Woody Aug 27 '18 at 04:35