如果你有一个序列:
example='abcdefabcdefabcdefg'
以及您的搜索:
searching_for='abc'
什么函数会给你一个包含所有位置的列表?
positions=[(0,2),(6-8),(12-14)]
我创建了一个窗口列表,将“example”分成 3 个,因此它来自“abc”、“bcd”、“cde”
windows=['abc', 'bcd', 'cde', 'def', 'efa', 'fab', 'abc', 'bcd', 'cde', 'def', 'efa', 'fab', 'abc', 'bcd', 'cde', 'def']
并使用了一个for循环
for i in windows:
if i == 'abc':
这就是我卡住的地方。 . .
请您参考如下方法:
您可以使用 regular expressions ;匹配对象带有位置信息 attached .使用 Python 2 的示例:
>>> import re
>>> example = 'abcdefabcdefabcdefg'
>>> for match in re.finditer('abc', example):
print(match.start(), match.end())
0 3
6 9
12 15
