lookup/map multiple keys in a dictionary and add to a list

Blair

My current code:

from nltk.tag import pos_tag, map_tag
search_term = 'quaker lemon banana oatmeal'
lst = []
search_term_words = search_term.split()
for w in search_term_words:
  if w in flavor_grocer_mapping:
      for flavor in flavor_grocer_mapping[w]:
          if flavor in search_term:
              lst.append((flavor, 'FLAVOR'))
              for x in search_term.replace(flavor, '').split(): 
                  if x in brand_grocer_mapping:
                      for brand in brand_grocer_mapping[x]:
                          if brand in search_term.replace(flavor, '').split():
                              lst.append((brand, 'BRAND'))
                              for word, tag in pos_tag(word_tokenize(search_term.replace(flavor, '').replace(brand, '').strip())):
                                  lst.append((word, map_tag('en-ptb', 'universal', tag)))

The result I got:

[('lemon', 'FLAVOR'), 
 ('quaker', 'BRAND'),
 ('banana', 'NOUN'), 
 ('oatmeal', 'NOUN'),
 ('banana', 'FLAVOR'),
 ('quaker', 'BRAND'),
 ('lemon', 'ADJ'),
 ('oatmeal', 'NOUN')]

My expected result is:

[('lemon', 'FLAVOR'),
 ('banana', 'FLAVOR'),
 ('quaker', 'BRAND'),
 ('oatmeal', 'NOUN')]

I know the problem is that the splitted words in search terms run recursively in the for loops. How can lookup/map a whole string that contains multiple keys in a dictionary? (For example, lemon and banana are in the search term string and they are the keys in the flavor_grocer_mapping dictionary.)

BernardL

Well, I tried to recreate flavor_grocer_mapping based on your results, your additional code to lookup is highly unnecessary. Instead, since you have a dictionary that corresponds to search_term_words as keys, do a proper dictionary lookup in a list comprehension.

search_term = 'quaker lemon banana oatmeal'
search_term_words = search_term.split()

#recreating your dictionary
flavor_grocer_mapping = [('lemon', 'FLAVOR'),
                         ('banana', 'FLAVOR'),
                         ('quaker', 'BRAND'),
                         ('oatmeal', 'NOUN')]
flavor_grocer_mapping = {k:v for (k,v) in flavor_grocer_mapping} 

#solution
results = [(word,flavor_grocer_mapping[word]) for word in search_term_words]
results
>>[('quaker', 'BRAND'),
 ('lemon', 'FLAVOR'),
 ('banana', 'FLAVOR'),
 ('oatmeal', 'NOUN')]

In the case of your provided example, I suggest your merge all the dictionaries to 1 instead of repeatedly looping over them.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

add keys and values to a dictionary from multiple objects by for loop

分類Dev

Assign list values to dictionary keys

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

How to add a list under a dictionary?

分類Dev

how to add items to the list that is in the dictionary

分類Dev

How to convert dictionary to dataframe when values of keys are list of list?

分類Dev

Split Python dictionary into multiple keys, dividing the values equally

分類Dev

Change multiple keys from dictionary, while doing timedelta operation in Python

分類Dev

Is it possible to have a same collection instance in a dictionary associated with multiple keys in swift?

分類Dev

How to add a list as value in dictionary Python

分類Dev

Search dictionary values and return List of keys meeting conditions

分類Dev

Convert list to dictionary with duplicate keys using dict comprehension

分類Dev

How to convert a dictionary to a list of keys, with repeat counts given by the values?

分類Dev

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

分類Dev

Swap keys for values in dictionary?

分類Dev

Strategy for dictionary with optional keys

分類Dev

Moving Keys in dictionary

分類Dev

How to update keys in dictionary?

分類Dev

How to unpack multiple dictionary objects inside list within a row of dataframe?

分類Dev

How to create a dictionary by iterating over a list, with same key multiple values?

分類Dev

How to add multiple dictionaries together from a dictionary generator to create single collated python dictionary

分類Dev

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

分類Dev

Converting pandas dataframe into dictionary where keys are index and values are list of column values

分類Dev

How to add list to dictionary by using index of list as key and item of list as value?

分類Dev

Sort dictionary keys where the key is also a dictionary

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

Related 関連記事

  1. 1

    How to add multiple values to dictionary keys in Python

  2. 2

    add keys and values to a dictionary from multiple objects by for loop

  3. 3

    Assign list values to dictionary keys

  4. 4

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  5. 5

    How to add a list under a dictionary?

  6. 6

    how to add items to the list that is in the dictionary

  7. 7

    How to convert dictionary to dataframe when values of keys are list of list?

  8. 8

    Split Python dictionary into multiple keys, dividing the values equally

  9. 9

    Change multiple keys from dictionary, while doing timedelta operation in Python

  10. 10

    Is it possible to have a same collection instance in a dictionary associated with multiple keys in swift?

  11. 11

    How to add a list as value in dictionary Python

  12. 12

    Search dictionary values and return List of keys meeting conditions

  13. 13

    Convert list to dictionary with duplicate keys using dict comprehension

  14. 14

    How to convert a dictionary to a list of keys, with repeat counts given by the values?

  15. 15

    How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

  16. 16

    Swap keys for values in dictionary?

  17. 17

    Strategy for dictionary with optional keys

  18. 18

    Moving Keys in dictionary

  19. 19

    How to update keys in dictionary?

  20. 20

    How to unpack multiple dictionary objects inside list within a row of dataframe?

  21. 21

    How to create a dictionary by iterating over a list, with same key multiple values?

  22. 22

    How to add multiple dictionaries together from a dictionary generator to create single collated python dictionary

  23. 23

    How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

  24. 24

    Converting pandas dataframe into dictionary where keys are index and values are list of column values

  25. 25

    How to add list to dictionary by using index of list as key and item of list as value?

  26. 26

    Sort dictionary keys where the key is also a dictionary

  27. 27

    Compare keys of dictionary with values of another dictionary

  28. 28

    Assigning values to keys in a mutable dictionary

  29. 29

    Match keys to values in dictionary in python

ホットタグ

アーカイブ