[Tutor] don't understand iteration

Alan Gauld alan.gauld at btinternet.com
Mon Nov 10 13:19:44 CET 2014


On 10/11/14 00:34, Clayton Kirkwood wrote:

>  if 'EST' in line or 'EDT' in line:  # look for Eastern Time
>    blah = re.search(r'<\w\w>(\w{3}\.)\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)
>    (month, day, time, ap, offset) = blah.group(1,2,3,4,5)
>
> <_sre.SRE_Match object; span=(0, 28), match='<BR>Nov. 09, 07:15:46 PM EST'>
>
>   PM Nov. EST 09 07

> blah.group(1,2,3,4,5), but this is problematic for me. I shouldn’t have
> to use that 1,2,3,4,5 sequence.

Why not? You have a hard coded search string so why not hard code
the corresponding group numbers? It's a lot more explicit and
hence reliable than using range() and it's faster too.

range() is great if you have to generate a long sequence (ie. tens or 
hundreds or more) of numbers or, more importantly, if you don't know
in advance how many numbers you need. But in your case its fixed by
the regex pattern you use.

>   range(5) which doesn’t work, list(range(5)) which actually lists the
> numbers in a list, and several others. As I read it, the search puts out
> a tuple.

No, it 'puts out' (ie returns) a Match object which is nothing like a 
tuple. You have to access the result data using the methods of the object.

blah.groups() returns all the groups as a tuple, which seems to be
what you want, so

(month, day, time, ap, offset) = blah.groups()

should do what you want.

> I couldn’t find a way to get the len of blah.

What would you expect the answer to be?
- The number of matches?
- The length of the matched strings?
- the lengths of the individual groups?

blah.span(0)

might be what you want but I'm not sure.

Incidentally, I don't think this has much to do with iteration as in the 
subject line, it's more about using regular expressions.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list