检查单词列表中单词的子串匹配 [英] Check substring match of a word in a list of words

查看:33
本文介绍了检查单词列表中单词的子串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个词是否在词列表中.

I want to check if a word is in a list of words.

word = "with"
word_list = ["without", "bla", "foo", "bar"]

我尝试了 if word in set(list),但由于 in 匹配字符串而不是项目,因此没有产生想要的结果.也就是说,"with"word_list中的任何一个词都匹配,但仍然是if "with" in set(list) 会说 True.

I tried if word in set(list), but it is not yielding the wanted result due to the fact in is matching string rather than item. That is to say, "with" is a match in any of the words in the word_list but still if "with" in set(list) will say True.

有什么比手动遍历 list 更简单的检查方法?

What is a simpler way for doing this check than manually iterate over the list?

推荐答案

您可以:

found = any(word in item for item in wordlist)

检查每个单词是否匹配,如果匹配则返回 true

It checks each word for a match and returns true if any are matches

这篇关于检查单词列表中单词的子串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆