Jun-11-2022, 12:57 PM
Hello,
I'm going through a Python course and there's a section on extracting data using regular expressions in which the presenter shows the following code using re.findall within a for loop:
1 ) The presenter refers to "stuff" (first introduced on line 6) as a list and the code on line 8 appears to be applying the float method to the first item in the list. My question is how did Python "know" that stuff was a list without a line of code before line 6 stating "stuff=[]"? Is the list somehow defined or created within the square brackets or parentheses used in line 6?
2 ) Doesn't the continue statement on line 7 need to be on its own line rather than at the end of line 7? I'm not sure if I've seen it placed on the same line before and am unclear as to whether this is an acceptable variation, a change from one version of Python to another, or something else.
Thanks so much in advance for your help.
I'm going through a Python course and there's a section on extracting data using regular expressions in which the presenter shows the following code using re.findall within a for loop:
import re hand = open ('mbox-short.txt') numlist = list() for line in hand: line = line.rstrip() stuff = re.findall('^X-DSPAM-Confidence: ([0-9.]+)', line) if len(stuff) !=1 : continue num = float(stuff[0]) numlist.append(num) print('Maximum:', max(numlist))I have two (unrelated, I think) questions about the above:
1 ) The presenter refers to "stuff" (first introduced on line 6) as a list and the code on line 8 appears to be applying the float method to the first item in the list. My question is how did Python "know" that stuff was a list without a line of code before line 6 stating "stuff=[]"? Is the list somehow defined or created within the square brackets or parentheses used in line 6?
2 ) Doesn't the continue statement on line 7 need to be on its own line rather than at the end of line 7? I'm not sure if I've seen it placed on the same line before and am unclear as to whether this is an acceptable variation, a change from one version of Python to another, or something else.
Thanks so much in advance for your help.