我在做一个for循环。我在一个列表中循环,找到一个包含正则表达式模式的特定字符串。一旦我找到了这条线,我就需要搜索某个图案的下一条线。我需要存储这两行代码,以便能够解析出它们的时间。我已经创建了一个计数器,以便在外部for循环工作时跟踪列表的索引号。我能用这样的结构来找到我需要的第二条线吗?在
index = 0
for lineString in summaryList:
match10secExp = re.search('taking 10 sec. exposure', lineString)
if match10secExp:
startPlate = lineString
for line in summaryList[index:index+10]:
matchExposure = re.search('taking \d\d\d sec. exposure', line)
if matchExposure:
endPlate = line
break
index = index + 1
代码运行了,但我没有得到我想要的结果。在
谢谢。在
应该是的
^{pr2}$根据您的具体需要,您可以在列表上使用一个迭代器,或者使用其中两个迭代器作为mae by itertools.tee。一、 例如,如果您只想在第一个模式之后的行中搜索第二个模式,那么一个迭代器就可以:
这将而不是搜索从
^{pr2}$aline
到结束somestart
的行,只搜索someend
的行。如果您需要为这两个目的搜索它们,即让theiter
本身保持原样,那么tee
可以帮助您:这是关于
tee
的一般规则的一个例外,文档给出了:因为}的推进发生在代码不相交的部分,并且{}总是在需要时重新重建(因此{}的推进与此无关)。在
theiter
和{相关问题 更多 >
编程相关推荐