检查列表中的任何字符串是否出现在较大的字符串上

时间:2019-04-04 15:12:04

标签: python loops

我有一个字符串列表。我想检查该列表中的任何字符串是否出现在保存在字符串var中的更大文档中。

我知道可以很容易地通过循环来完成此操作,但是我将执行此操作很多次(还有另一个循环),所以我想知道是否有更有效的方法来代替for循环。

我的方法是这样的:

main_words = ... # List of words I want to check
tweet = ... # String containing the text I want to check for word appearance

for word in main_words:
    if word in tweet:
        .......

1 个答案:

答案 0 :(得分:2)

您可以使用集合获取此信息:

text = """I have a list of strings. I would like to check if any of the strings of 
that list appears on a bigger document saved on a string var.

I know this can easily be done with a loop, but I will be doing this operation so 
many times (and another loops apart of this) so I was wondering if there is any
more efficient way to do it instead of a for loop."""

words = set(["would","this","do","if","supercalifragelisticexpialigetic"])

text_words = text.split()

# show all that are in it
print(words.intersection(text_words))   # words & set(text_words)
# show all that are not in it
print(words.difference(text_words))     # words - set(text_words)

输出:

set(['this', 'do', 'would', 'if'])               # words & set(text_words)
set(['supercalifragelisticexpialigetic'])        # words - set(text_words)

也要获取计数:

from collections import Counter

counted = Counter(text_words)

for w in words:
    print(w, counted.get(w))

输出:

do 1
would 1
supercalifragelisticexpialigetic None
if 2
this 2