检查另一个字符串中是否连续存在多个字符串

时间:2018-07-27 10:42:01

标签: python python-2.7

如何检查数组中的任何字符串是否在另一个字符串中?

赞:

a = ['the', 'you']
b = ['is', 'are']
c = ['now', 'not']
str = "the game is now"
if "the" in str:
 print "the strings found in str"
else:
 print "the strings found in str"

现在,我想检查在“ a”中是否在“ b”中找到“您”之后是否在此之前。任何帮助,请亲爱的?

1 个答案:

答案 0 :(得分:0)

可以使用正则表达式来做到这一点。

import re

a = ['your', 'game', "is", "over"]
regex = ".+".join(a)
print(regex)
if re.match(regex, "your game my dear player is over"):
    print("It's over")

带有索引:

a = ['your', 'game', "is", "over"]

list_of_words = " game my dear player is over".split()
is_correct = True
for i, value in enumerate(a):
    if i < 1:
        continue
    if value not in list_of_words or a[i - 1] not in list_of_words:
        is_correct = False
        break
    if list_of_words.index(value) < list_of_words.index(a[i - 1]):
        is_correct = False
if is_correct:
    print("It's over")