I'm trying to iterate over items in a list generated by split() in python 3.4, and I can't understand why it's not working like I expect it to. Here's the code:
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))
I expected this to look at each of the four things created by seqdes.split(',') and if it found the regex match, append the two digits found at the start of each line to the designated list. Instead, I get:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
So I see that the code is trying to run the secondbaselist.append piece on an item from the seqdes.split list that doesn't contain "advances to 2nd" anywhere, but I don't know why it's doing that. Since the if statement is false there, I wouldn't think it would try the append part; obviously then I'm not getting the desired behavior from the if statements, but I don't understand why.
I have also tried this with if item.find("advances to 1st")
, etc, with no change. What am I missing?