python - 检查多个字符串是否连续存在于另一个字符串中
问题描述
如何检查数组中的任何字符串是否存在于另一个字符串中?
喜欢:
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”中找到“you”,然后在“b”中找到“are”,而不是在此之前。任何帮助,请亲爱的?
解决方案
可以使用正则表达式来做到这一点。
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")
推荐阅读
- node.js - 如何将 Elasticsearch 与 Sails 集成
- reactjs - 递归地克隆和修改 React 中的子组件 - 包括其他组件的子组件
- java - 使用正则表达式需要去除特定的特殊字符
- java - 如何从firebase android检索子数据
- lua - Garrys Mod Lua 问题(检查相等并打印)
- javascript - 映射数组返回未定义的值
- java - 使用 Java SDK,如何在 Azure 存储文件服务中的文件上设置元数据?
- google-data-studio - 谷歌数据工作室
- javascript - 如何将数据从 Node.JS Web 应用程序传输到另一个网站/服务器上的 JavaScript 客户端?
- html - 具有不同纵横比内容的响应式 iframe - 让解决方案寻找更好的解决方案