在 Python for 循环中索引列表

发布于 2024-09-06 22:57:31 字数 608 浏览 5 评论 0原文

我正在 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

代码运行,但我没有得到我想要的结果。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-09-13 22:57:31
matchExposure = re.search('taking \d\d\d sec. exposure', lineString)

可能应该是

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

should probably be

matchExposure = re.search('taking \d\d\d sec. exposure', line)
九厘米的零° 2024-09-13 22:57:31

根据您的具体需求,您可以仅使用列表上的迭代器,或者使用其中两个作为 itertools.tee。即,如果您想要在第一个模式之后的行中搜索第二个模式,则单个迭代器即可:

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搜索行code> 到 somestart 的结尾 anothersomeendonly。如果您需要出于这两个目的搜索它们,即为外部循环保留 theiter 本身完整,那么 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() 进行了分割,
不应使用原始可迭代对象
其他任何地方;否则,可迭代
没有 T 恤也能取得进步
被通知的对象。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文