在Python中,在列表上迭代三个IF语句时出现意外行为

在Python中,在列表上迭代三个IF语句时出现意外行为,python,regex,list,if-statement,python-3.x,Python,Regex,List,If Statement,Python 3.x,我试图在Python3.4中迭代split()生成的列表中的项目,但我不明白为什么它不能像我期望的那样工作。代码如下: seqdes = '48 Marshall McDonald advances to 1st (single), 43 Nicholas Boggan advances to 2nd (48), 48 Marshall McDonald advances to 2nd (wild pitch), 43 Nicholas Boggan advances to 3rd (wild

我试图在Python3.4中迭代split()生成的列表中的项目,但我不明白为什么它不能像我期望的那样工作。代码如下:

seqdes = '48 Marshall McDonald advances to 1st (single), 43 Nicholas Boggan advances to 2nd (48), 48 Marshall McDonald advances to 2nd (wild pitch), 43 Nicholas Boggan advances to 3rd (wild pitch)'
firstbaselist = []
secondbaselist = []
thirdbaselist = []

for item in seqdes.split(','):
    if re.compile('.*advances to 1st.*').match(item):
        firstbaselist.append(re.compile('\d\d').match(item).group(0))
    if re.compile('.*advances to 2nd.*').match(item):
        secondbaselist.append(re.compile('\d\d').match(item).group(0))
    if re.compile('.*advances to 3rd.*').match(item):
        thirdbaselist.append(re.compile('\d\d').match(item).group(0))
我希望这可以查看seqdes.split(“,”)创建的四个东西中的每一个,如果它找到了正则表达式匹配项,则将在每行开头找到的两个数字附加到指定的列表中。相反,我得到:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
回溯(最近一次呼叫最后一次):
文件“”,第5行,在
AttributeError:“非类型”对象没有属性“组”
因此,我看到代码试图在seqdes.split列表中的一个项目上运行secondbaselist.append片段,该项目在任何地方都不包含“advancesto2nd”,但我不知道它为什么这样做。既然if语句在这里是假的,我想它不会尝试append部分;很明显,我没有从if语句中得到想要的行为,但我不明白为什么


我也尝试过使用
if item.find(“前进到第1位”)
等,但没有任何更改。我遗漏了什么?

错误是因为您使用了
re.match
而不是
re.search
。此处解释了
re.match
re.search
之间的区别:

错误的原因是re.match文档中的这一行:

如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的MatchObject实例。如果字符串与模式不匹配,则返回None;请注意,这与零长度匹配不同

拆分字符串时,字符串中的第二项是
'43 Nicholas Boggan前进到第二(48)
,它有一个空格开始。由于该空间不是正则表达式的一部分,
re.match
将失败并返回
None
。那么这条线呢

secondbaselist.append(重新编译('\d\d').match(项).group(0))

变为
None.group(0)
,且无类型对象没有属性组

使用
re.search
可以解决此问题。

尝试以下方法:

import re
seqdes = '48 Marshall McDonald advances to 1st (single), 43 Nicholas Boggan advances to 2nd (48), 48 Marshall McDonald advances to 2nd (wild pitch), 43 Nicholas Boggan advances to 3rd (wild pitch)'
firstbaselist = []
secondbaselist = []
thirdbaselist = []

for item in seqdes.split(','):
    if 'advances to 1st' in item:
        firstbaselist.append(re.search(r'(\d\d)',item).group(0))
    elif 'advances to 2nd' in item:
        secondbaselist.append(re.search(r'(\d\d)',item).group(0))
    elif 'advances to 3rd' in item:
        thirdbaselist.append(re.search(r'(\d\d)',item).group(0))

print firstbaselist
print secondbaselist
print thirdbaselist
给出:

['48']
['43', '48']
['43']

您有一个输入错误:
用于seqdes中的项。拆分(“,”)“
末尾有一个
,而不是
。(那不是你的错误)呵呵。修好了。但是,是的,这是从python窗口到这里的一个转录错误,而不是问题的根源。您需要有一个括号组来捕获,并且还应该使用原始字符串:
re.compile(r'(\d\d))…
。不确定您为什么一直在编译,
re.search()
有什么问题?想一想,对于
if
条件,您实际上不需要RE,in中的
应该可以。我同意RE在if条件下是不必要的,但是尝试一个非正则替代项item.find(“”)也不起作用。在此之前,我没有任何使用regex的经验,因此,如果我在这方面做了一些错误和/或效率低下的事情,我也不会感到惊讶。事实上,解决方案是使用
re.search
而不是
re.match
——下面的答案正是我所需要的。不过,谢谢你阅读我的问题!这非常有效-谢谢!为了确认这就是解决方案,我还使用re.match(item.strip())对其进行了测试,该方法去掉了前导空格,并且也取得了成功。