在字典中查找/映射多个键并添加到列表

时间:2018-11-26 05:09:20

标签: python list dictionary

我当前的代码:

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)))

我得到的结果:

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

我的预期结果是:

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

我知道问题是搜索字词中的拆分词在for循环中递归运行。如何在字典中查找/映射包含多个键的整个字符串? (例如,lemonbanana在搜索词字符串中,它们是flavor_grocer_mapping字典中的键。)

1 个答案:

答案 0 :(得分:2)

好吧,我尝试根据您的结果重新创建flavor_grocer_mapping,因此无需额外的查找代码。相反,由于您有一个与search_term_words对应的字典作为键,因此请在列表理解中进行适当的字典查找。

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')]

对于您提供的示例,我建议您将所有词典合并为1,而不是重复循环遍历它们。