Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.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 3.x 搜索列表中的项目序列_Python 3.x_List Comprehension - Fatal编程技术网

Python 3.x 搜索列表中的项目序列

Python 3.x 搜索列表中的项目序列,python-3.x,list-comprehension,Python 3.x,List Comprehension,有没有一种简单的方法可以搜索列表中的字符串序列?例如: testlist = [a,b,c,d,e,f,g,a,b,c,d,j,k,j] 我想搜索序列abc,并得到返回的索引。因此,为了澄清我要搜索的字符串是否由列表中的多个元素组成。对于某些上下文:我有一个包含数据块的列表,我想知道每个数据块有多大,因此在列表中搜索一个重复出现的字符串。有很多好的方法:KMP、Boyer Moore、Rabin Karp。如果处理字符(str.index在CPython:)中实现了Boyer-Moore算法

有没有一种简单的方法可以搜索列表中的字符串序列?例如:

testlist = [a,b,c,d,e,f,g,a,b,c,d,j,k,j] 
我想搜索序列
abc
,并得到返回的索引。因此,为了澄清我要搜索的字符串是否由列表中的多个元素组成。对于某些上下文:我有一个包含数据块的列表,我想知道每个数据块有多大,因此在列表中搜索一个重复出现的字符串。

有很多好的方法:KMP、Boyer Moore、Rabin Karp。如果处理字符(
str.index
在CPython:)中实现了Boyer-Moore算法),则可以在
''.join(L)
上使用内置的
str.index
函数

但在大多数情况下,朴素的算法已经足够好了。检查
草堆的每个索引
,以找到

>>> a, b, c, d, e, f, g, j, k = [object() for _ in range(9)]
>>> haystack = [a, b, c, d, e, f, g, a, b, c, d, j, k, j]
>>> needle = [a, b, c]
>>> for i in range(len(haystack)-len(needle)+1):
...     if haystack[i:i+len(needle)] == needle:
...             print(i)
... 
0
7
复杂性是O(| haystack |*| needle |)。

有很多好处:KMP、博耶·摩尔、拉宾·卡普。如果处理字符(
str.index
在CPython:)中实现了Boyer-Moore算法),则可以在
''.join(L)
上使用内置的
str.index
函数

但在大多数情况下,朴素的算法已经足够好了。检查
草堆的每个索引
,以找到

>>> a, b, c, d, e, f, g, j, k = [object() for _ in range(9)]
>>> haystack = [a, b, c, d, e, f, g, a, b, c, d, j, k, j]
>>> needle = [a, b, c]
>>> for i in range(len(haystack)-len(needle)+1):
...     if haystack[i:i+len(needle)] == needle:
...             print(i)
... 
0
7

复杂性是O(| haystack |*| needle |)。

转换为字符串和搜索。我发现Knuth的论文“令人兴奋的阅读。还有其他方法吗?我需要重新出现的字符串的位置。我将阅读“字符串中的快速模式匹配”谢谢!这是一个变量名列表,不是字符串。转换为字符串并搜索。我发现Knuth的论文“令人兴奋。还有其他方法吗?”?我需要重新出现的字符串的位置。我将阅读“字符串中的快速模式匹配”谢谢!这是变量名列表,而不是字符串。