Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 for循环中的列表编制索引_Python_Nested Loops - Fatal编程技术网

为Python for循环中的列表编制索引

为Python for循环中的列表编制索引,python,nested-loops,Python,Nested Loops,我在做一个for循环中的for循环。我在一个列表中循环,找到一个包含正则表达式模式的特定字符串。一旦我找到了线,我需要搜索以找到某个模式的下一行。我需要存储这两行,以便能够解析出它们的时间。我已经创建了一个计数器来跟踪外部for循环工作时列表的索引号。我可以用这样的结构来找到我需要的第二条线吗 index = 0 for lineString in summaryList: match10secExp = re.search('taking 10 sec. exposure',

我在做一个for循环中的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
代码运行,但我没有得到我想要的结果

谢谢

matchExposure = re.search('taking \d\d\d sec. exposure', lineString)
应该是

matchExposure = re.search('taking \d\d\d sec. exposure', line)

根据您的具体需要,您可以在列表中使用一个迭代器,或者使用其中两个作为maeby。也就是说,如果您只想在第一个模式之后的行中搜索第二个模式,那么单个迭代器将执行以下操作:

theiter = iter(thelist)

for aline in theiter:
  if re.search(somestart, aline):
    for another in theiter:
      if re.search(someend, another):
        yield aline, another  # or print, whatever
        break
这不会在
aline
到另一个
结尾的
行中搜索
somestart
,只搜索
someend
。如果您需要出于两个目的对其进行搜索,即保持
iter
自身在外部循环中保持不变,则
tee
可以帮助:

for aline in theiter:
  if re.search(somestart, aline):
    _, anotheriter = itertools.tee(iter(thelist))
    for another in anotheriter:
      if re.search(someend, another):
        yield aline, another  # or print, whatever
        break
这是文档给出的关于
tee
的一般规则的例外:

一旦
tee()
进行拆分,则 不应使用原始iterable 其他任何地方;否则,这个问题将无法解决 没有发球台就可以晋级 正在通知的对象


因为
iter
anotheriter
的推进发生在代码的不相交部分,并且
anotheriter
总是在需要时重新构建(因此
iter
的推进同时并不相关).

您可能还希望包含外部循环的代码。您可以不手动计数,而是执行以下操作:
对于索引,枚举中的行(summaryList):
我认为您是对的,但我已解决了这一问题,它仍然无法生成正确的输出。我在最里面的if语句中放了一个打印行,但没有打印任何内容。您要查找的第二行是否始终是曝光时间的3位数字?(另外,您可以使用
\d{3}
而不是
\d\d