擅长:python、mysql、java
<p>我正在使用您的原始帖子数据。<br/>
第一个拆分项<br/>
再次检查字典中的关联项<br/>
三合一</p>
<pre><code>terms =["cheese omelette","turkey sandwich","bean soup",]
dictionary ={'turkey': 'meat', 'cheese': 'dairy', 'sandwich': 'bread', 'beef': 'meat', 'omelette': 'eggs', 'bean': 'legume', 'carrot': 'vegetable', 'milk': 'dairy'}
res = set( term +' '+ cat for term in terms for cat in set([ dictionary .get(word,'') for word in term.split()]) if cat)
for i in res:
print i
output:
cheese omelette dairy
bean soup legume
turkey sandwich meat
turkey sandwich bread
cheese omelette eggs
</code></pre>