>>> listA = ["A","B","C"]
>>> listB = ["B","D","E"]
>>> stringB = "There is A loud sound over there"
>>> lis = stringB.split()
>>> any(item in listA or item in lis for item in listA)
True
如果listB很大,或者{}返回的列表很大,则应首先将其转换为sets,以提高复杂性:
^{pr2}$
如果要在该字符串中搜索多个单词,请使用regex:
>>> import re
>>> listA = ["A","B","C"]
>>> listB = ["B","D","E"]
>>> stringB = "There is A loud sound over there"
>>> any(item in listA or re.search(r'\b{}\b'.format(item),stringB)
for item in listA)
您可以在这里使用
any
,any
将短路并在找到第一个匹配项时停止。在如果}返回的列表很大,则应首先将其转换为
^{pr2}$listB
很大,或者{sets
,以提高复杂性:如果要在该字符串中搜索多个单词,请使用
regex
:要查找两个列表的重叠,可以执行以下操作:
在
^{pr2}$if
语句中,您只需执行以下操作:但是,如果
listA
中的任何项超过一个字母,则set方法不适用于查找stringB
中的项,因此最好的替代方法是:相关问题 更多 >
编程相关推荐