Python:如何确定字符串中是否存在单词列表
给定一个列表[“一”,“二”,“三”],如何确定每个单词是否存在于指定字符串中?
单词列表很短(在我的情况下不到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 = [“一天”,“第二天”].也许我应该提出另一个问题. 解决方法
这个功能是由彼得·吉布森(Peter Gibson)(下文)发现的,在这里是最好的答案.这对存储器中可能存在的数据集是有好处的(因为它创建了一个字串从被搜索的列表,然后一组这些字)
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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |