您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:如何确定字符串中是否存在单词列表

5b51 2022/1/14 8:22:54 python 字数 4086 阅读 592 来源 www.jb51.cc/python

给定一个列表[“一”,“二”,“三”],如何确定每个单词是否存在于指定字符串中? 单词列表很短(在我的情况下不到20个字),但要搜索的字符串是非常大的(每个运行40万个字符串) 我目前的实施使用re来寻找比赛,但我不知道是否是最好的方法. import re word_list = ["one", "two", "three"] regex_string = "(?<=\W)(%s)(?=\W)"

概述

单词列表很短(在我的情况下不到20个字),但要搜索的字符串是非常大的(每个运行40万个字符串)

我目前的实施使用re来寻找比赛,但我不知道是否是最好的方法.

import re
word_list = ["one","two","three"]
regex_string = "(?<=\W)(%s)(?=\W)" % "|".join(word_list)

finder = re.compile(regex_string)
string_to_be_searched = "one two three"

results = finder.findall(" %s " % string_to_be_searched)
result_set = set(results)
for word in word_list:
    if word in result_set:
        print("%s in string" % word)

我的解决方案中的问题

>它将搜索到字符串的末尾,尽管字可能出现在字符串的前半部分
>为了克服前瞻断言的限制(我不知道如何表达“当前匹配前的字符应该是非字符的字符,或字符串的开始”),我在字符串之前和之后添加了额外的空格我需要搜索.
>其他性能问题由前瞻性断言引入?

可能更简单的实现:

>只是循环通过单词列表,并在string_to_be_searched中做一个if单词.但是如果你正在寻找“三”的话,它不能处理“三人”
>使用一个正则表达式搜索一个单词.仍然我不知道性能,以及多次搜索字符串的潜力.

更新:

我接受了Aaron Hall的答案https://stackoverflow.com/a/21718896/683321,因为根据Peter Gibson的基准https://stackoverflow.com/a/21742190/683321,这个简单的版本有最好的表现.如果您对此问题感兴趣,您可以阅读所有答案并获得更好的观点.

其实我忘了在我原来的问题上提到另一个约束.该词可以是短语,例如:word_list = [“一天”,“第二天”].也许我应该提出另一个问题.

def words_in_string(word_list,a_string):
    return set(word_list).intersection(a_string.split())

用法

my_word_list = ['one','two','three']
a_string = 'one two three'
if words_in_string(my_word_list,a_string):
    print('One or more words found!')

哪个打印一个或找到的单词!到stdout.

它确实返回发现的实际字词:

for word in words_in_string(my_word_list,a_string):
    print(word)

打印出来

three
two
one

For data so large you can’t hold it in memory,the solution given in this answer would be very performant.

总结

以上是编程之家为你收集整理的Python:如何确定字符串中是否存在单词列表全部内容,希望文章能够帮你解决Python:如何确定字符串中是否存在单词列表所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶