Python:正则表达式 findall 返回一个列表,为什么尝试访问列表元素 [0] 会返回错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14992787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:02:21  来源:igfitidea点击:

Python: Regex findall returns a list, why does trying to access the list element [0] return an error?

pythonregex

提问by Louis93

Taken from the documentation, the following is a snippet showing how the regex method findall works, and confirms that it does return a list.

以下摘自文档的片段,显示了正则表达式方法 findall 的工作原理,并确认它确实返回了一个列表。

re.findall(r"\w+ly", text)
['carefully', 'quickly']

However the following code fragment generates an out of bounds error (IndexError: list index out of range)when trying to access the zeroth element of the list returned by findall.

但是,以下代码片段IndexError: list index out of range在尝试访问 findall 返回的列表的第零个元素时会生成越界错误 ( )。

Relevant Code Fragment:

相关代码片段:

population = re.findall(",([0-9]*),",line)
x = population[0]
thelist.append([city,x])

Why does this happen?

为什么会发生这种情况?

For some more background, here's how that fragment fits into my entire script:

有关更多背景信息,以下是该片段如何适合我的整个脚本:

import re

thelist = list()
with open('Raw.txt','r') as f:
    for line in f:
        if line[1].isdigit():
            city = re.findall("\"(.*?)\s*\(",line)
            population = re.findall(",([0-9]*),",line)
            x = population[0]
            thelist.append([city,x])

with open('Sorted.txt','w') as g:
    for item in thelist:
        string = item[0], ', '.join(map(str, item[1:]))
        print string

EDIT: Read comment below for some background on why this happened. My quick fix was:

编辑:阅读下面的评论,了解为什么会发生这种情况的一些背景。我的快速修复是:

if population: 
        x = population[0]
        thelist.append([city,x])

采纳答案by nneonneo

re.findallwill return an empty list if there are no matches:

re.findall如果没有匹配项,将返回一个空列表:

>>> re.findall(r'\w+ly', 'this does not work')
[]

回答by wim

re.findallcan return you an empty list in the case where there was no match. If you try to access [][0]you will see that IndexError.

re.findall在没有匹配的情况下可以返回一个空列表。如果您尝试访问,[][0]您将看到IndexError

To take into account no matches, you should use something along the lines of:

要考虑没有匹配项,您应该使用以下内容:

match = re.findall(...)
if match:
  # potato potato

回答by James Shrum

I had this same issue. The solution seems very simple, and I don't know why I wasn't considering it.

我有同样的问题。解决方案看起来很简单,我不知道为什么我没有考虑它。

if match:

instead of

代替

if match[0]: