views:

93

answers:

2

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
    index = index + 1

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

Thanks.

+1  A: 
matchExposure = re.search('taking \d\d\d sec. exposure', lineString)

should probably be

matchExposure = re.search('taking \d\d\d sec. exposure', line)
Amber
I think you're right, but I fixed that and it still won't produce the right output. I put a print line in the innermost if statement and nothing is being printed.
Hannah
Is the second line you're looking for always 3 digits for the exposure time? (Also, you can use `\d{3}` instead of `\d\d\d`.)
Amber
+1  A: 

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).

Alex Martelli