<p>我需要我的脚本为不同的词给出定义</p>
<p>我使用一个循环来查找字符串(X)和数组中的项之间的匹配</p>
<pre><code>if any(i in X for i in ('coconut, Coconut')):
print("found coconut")
if any(i in X for i in ('nut', 'Nut')):
print("found nut")
</code></pre>
<p>问题是,当数组X中的项是一个包含另一个字的字(例如couch&;nut)时,两个打印都会执行</p>
<p>我如何确保当数组X中有一个名为couch的项目时,我只得到couch的打印,而不是nut的打印</p>
<p>我将永远感激你的帮助</p>
<p>您可以将字符串<code>X</code>转换为一个集合:</p>
<pre><code>sx = set(X.split()) # to get the words you may use a regex, depending of how X looks
if sx & {'coconut, Coconut'}:
print("found coconut")
</code></pre>