开发者

index a list in a Python for loop

I'm making a for loop within a for loop. I'm looping through a list and finding a specific string that contains a regular expression pattern. Once I find the line, I need to search to find the next line of a certain pattern. I need to store both lines to be able to parse out the time for them. I've created a counter to keep track of the index number of the list as the outer for loop works. Can I use a construction like this to find the second line I need?

 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
    开发者_如何学Pythonindex = index + 1

The code runs, but I'm not getting the result I'm looking for.

Thanks.


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

should probably be

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


Depending on your exact needs, you can just use an iterator on the list, or two of them as mae by itertools.tee. I.e., if you want to search lines following the first pattern only for the second pattern, a single iterator will do:

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

This will not search lines from aline to the ending another for somestart, only for someend. If you need to search them for both purposes, i.e., leave theiter itself intact for the outer loop, that's where tee can help:

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

This is an exception to the general rule about tee which the docs give:

Once tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being informed.

because the advancing of theiter and that of anotheriter occur in disjoint parts of the code, and anotheriter is always rebuilt afresh when needed (so the advancement of theiter in the meantime is not relevant).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜