gpt4 book ai didi

python - 检查字符串的一部分是否在列表中的有效方法

转载 作者:行者123 更新时间:2023-11-28 20:05:47 25 4
gpt4 key购买 nike

我有一个巨大的字符串,例如:

The Dormouse's story. Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well....badword...

我有一个大约 400 个坏词的列表:

bad_words = ["badword", "badword1", ....]

检查文本是否包含来自坏词列表的坏词的最有效方法是什么?

我可以像这样遍历文本和列表:

for word in huge_string:
for bw in bad_words_list:
if bw in word:
# print "bad word is inside text"...

但这在我看来是 90 年代的..

更新:坏词是单个词。

最佳答案

将您的文本转换为一组单词并计算其与一组不良单词的交集将为您带来分摊速度:

text  = "The Dormouse's story. Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well....badword..."

badwords = set(["badword", "badword1", ....])

textwords = set(word for word in text.split())
for badword in badwords.intersection(textwords):
print("The bad word '{}' was found in the text".format(badword))

关于python - 检查字符串的一部分是否在列表中的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com