[Tutor] don't understand iteration

Clayton Kirkwood crk at godblessthe.us
Mon Nov 10 05:22:29 CET 2014

>-----Original Message-----
>From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>Behalf Of Peter Otten
>Sent: Sunday, November 09, 2014 5:47 PM
>To: tutor at python.org
>Subject: Re: [Tutor] don't understand iteration
>Clayton Kirkwood wrote:
>> I have the following code:
>>          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) This
>> works fine, but in the (month... line, I have 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. I tried to use many alternatives using:  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. I
>> was hoping to just assign the re.search to month, day, time, ap,
>> offset directly. Why wouldn't that work? Why won't a range(5) work? I
>> couldn't find a way to get the len of blah.
>> What am I missing?
>While the direct answer would be
>month, day, time, ap, offset = blah.group(*range(1,6))
>there is also the groups() method
>month, day, time, ap, offset = blah.groups()
>which is appropriate when you want to unpack all capturing groups.

Still seems odd to me. Blah is a tuple, and would think that there should be
a natural way to pull it apart. One can iterate across a string or list
easily, why not a tuple. I also would have thought that a range(5) would
have worked. Isn't it an iterable?

Thanks for the insight...


>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:

More information about the Tutor mailing list