<p>我会用一个标志来解决这个问题,如果它被发现的话,它会被标记出来。你知道吗</p>
<p>请记住,我在键的名称和值列表之间进行了切换,以使其更符合逻辑,并更改了“I”和“j”以使名称具有更好的含义。你知道吗</p>
<p>你可以保留你的名字,如果你想,只添加两行关于'找到'。你知道吗</p>
<pre><code>values = ['well done', 'come on', 'going to', 'c', 'D', 'm', 'l', 'o']
keys = ['well', 'going', 'come', 'D']
category = []
for index, value in enumerate(values):
found = False
for key in keys:
if key in value:
category.append(key)
found = True
break
if not found:
category.append("other")
print(category)
</code></pre>
<p>方案2:</p>
<pre><code>values = ['well done', 'come on', 'going to', 'c', 'D', 'm', 'l', 'o']
keys = ['well', 'going', 'come', 'D']
category = []
for index, value in enumerate(values):
for key in keys:
if key in value:
category.append(key)
break
else:
category.append("other")
print(category)
</code></pre>
<p>在我看来,选项2是解决问题的一种更优雅的方法。^在<code>for</code>之后的{<cd1>}将被触发,以防<code>for</code>循环结束而没有击中途中的<code>break</code>。你知道吗</p>
<p>打印:<code>['well', 'come', 'going', 'other', 'D', 'other', 'other', 'other']
</code></p>
<p>关于你做错了什么-你在查看键列表而不是“值”列表时添加了“其他”。你知道吗</p>