表中单词的标记化senten

2024-04-19 02:18:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个单词/用具的清单

appliances = ['tv', 'radio', 'oven', 'speaker']

我还有一个句子,我已经标记了

sent = ['We have a radio in the Kitchen']
sent1 = word_tokenize[sent]

我想说,如果电器是在sent1然后打印是,否则打印否。我做了下面的,但不断得到作为打印否

if any(appliances) in sent1:
    print ('yes')
else:
    print ('no')

有没有更好的办法


Tags: in标记havetv单词句子sentwe
1条回答
网友
1楼 · 发布于 2024-04-19 02:18:23

试试这样的

appliances = ['tv', 'radio', 'oven', 'speaker']
sent = ['We have a radio in the Kitchen']
sent1 = list(sent[0].split())

if any([app in sent1 for app in appliances]):
    print ('yes')
else:
    print ('no')

根据@tobias\u k评论编辑

使用惰性评估

if any(app in sent1 for app in appliances):
    print ('yes')
else:
    print ('no')

编辑:基于@ben121评论

如果你想看看你的句子里有没有电器,你可以这样做

[app for app in appliances if app in sent1]

相关问题 更多 >