Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在collections.deque中查找子字符串_Python - Fatal编程技术网

Python 在collections.deque中查找子字符串

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

我试图在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 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)