我们如何有效地检查字符串列表是否包含来自另一个字符串列表的单词?(How can we efficiently check whether a list of string contains a wo

编程入门 行业动态 更新时间:2024-10-25 00:31:40
我们如何有效地检查字符串列表是否包含来自另一个字符串列表的单词?(How can we efficiently check whether a list of string contains a word from another list of strings?)

假设我有一个诅咒词列表

curseword = ['fuxx', 'die', 'damn']

如果我正在遍历一个句子列表(字符串列表),以检查该句子是否包含诅咒词。

text = [ ['i','am','a','boy'] , [....] , [....] ]

我试着做点什么

for i in curse_words: for t in text: if i in t: // exsits

但这似乎是错误和低效的。

我怎样才能有效地做到这一点?

Supposed I have a list of cursewords

curseword = ['fuxx', 'die', 'damn']

and if I am iterating through a list of sentence(list of string) to check if the sentence contains the curse word.

text = [ ['i','am','a','boy'] , [....] , [....] ]

I tried to do something like

for i in curse_words: for t in text: if i in t: // exsits

but it seems wrong and inefficient.

How can I do this efficiently?

最满意答案

将你的curseword列表转换为一个集合,然后用户set.intersection检查句子中的单词是否与cursword重叠。

In [10]: curseword = {'fuxx', 'die', 'damn'} In [11]: text = [ ['i','am','a','boy'], ['die']] In [21]: new_text = [int(bool(curseword.intersection(sent))) for sent in text] In [22]: new_text Out[22]: [0, 1]

Convert you curseword list to a set, and then user set.intersection to check if words in sentence overlap with cursword.

In [10]: curseword = {'fuxx', 'die', 'damn'} In [11]: text = [ ['i','am','a','boy'], ['die']] In [21]: new_text = [int(bool(curseword.intersection(sent))) for sent in text] In [22]: new_text Out[22]: [0, 1]

更多推荐

本文发布于:2023-08-06 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   列表   有效地   单词   efficiently

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!