Python 在collections.deque中查找子字符串
我试图在deque集合中搜索子字符串 我从集合中创建了一个deque构造 有没有办法在deque x中找到“this” 我尝试在x中查找('this'),但没有成功Python 在collections.deque中查找子字符串,python,Python,我试图在deque集合中搜索子字符串 我从集合中创建了一个deque构造 有没有办法在deque x中找到“this” 我尝试在x中查找('this'),但没有成功 from collections import deque x = deque('a', 'b', 'c') 'b' in x 输出:True x = deque('a', '# this #', 'c') 'this' in x 输出:False 想要一个方法来查找“this”您将在遍历deque时搜索子字符串: def
from collections import deque
x = deque('a', 'b', 'c')
'b' in x
输出:True
x = deque('a', '# this #', 'c')
'this' in x
输出:False
想要一个方法来查找“this”您将在遍历deque时搜索子字符串:
def find(substring, deque):
for s in deque:
if substring in s:
return True
return False
您需要测试集合中的每个项目。您可以在for循环中执行此操作:
x = deque('a', '# this #', 'c')
def find(collection):
for item in collection:
if 'this' in item:
return True
return False
或使用内置的,以及:
只检查队列中的第一个条目后,代码不会返回原样吗?
any('this' in item for item in x)
# regex approach
import re
from collections import deque
x = deque(['a', '# this #', 'c'])
substring= "this"
if re.search(substring, "".join(x)):
print(True)