[Tutor] don't understand iteration

Clayton Kirkwood crk at godblessthe.us
Tue Nov 11 00:08:23 CET 2014



>-----Original Message-----
>From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
>Behalf Of Alan Gauld
>Sent: Monday, November 10, 2014 4:20 AM
>To: tutor at python.org
>Subject: Re: [Tutor] don't understand iteration
>
>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.


Yes, great in a specific case such as this, but I am trying to learn the
general case: what if instead of 5 I had 25. I surely don't want to have to
type out every number in a program. Programs are there to do for me, not me
for it:<)) And again, this is why it would be useful to have a len or sizeof
a match object or tuple as argued below.

>
>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.

My mistake, search and match put out a match object. Match.group/s returns a
tuple for more than one argument returned.


>
>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.



Also of confusion, the library reference says:

Match objects always have a boolean value of True. Since match() and
search() return None when there is no match, you can test whether there was
a match with a simple if statement:

match = re.search(pattern, string)
if match:
    process(match)


blah =
re.search(r'<\w\w>(\w{3})\.\s+(\d{2}),\s+(\d{2}).+([AP]M)\s+(E[SD]T)', line)
>>> blah
<_sre.SRE_Match object; span=(45, 73), match='<BR>Nov. 09, 07:15:46 PM EST'>
>>> if blah: print(blah)
<_sre.SRE_Match object; span=(45, 73), match='<BR>Nov. 09, 07:15:46 PM EST'>
>>> if blah == True: print(blah)>
No print out

To me, this doesn't *appear* to be quite true.


>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.


Aye, that does it.


>> 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?


I would expect len(sizeof, whatever)(blah) to return the number of (in this
case) matches, so 5. Doing a search suggests what is important: the number
of matches. Why else would you do a search, normally.
That could then be used in the range()
It would be nice to have the number of arguments.
I would expect len(blah.group()) to be 5, because that is the relevant
number of elements returned from group. And that is the basic thing that
group is about; the groups, what they are and how many there are. I
certainly wouldn't want len(group) to return the number of characters, in
this case, 28 (which it does:>{{{


>>> blah.group()
'<BR>Nov. 09, 07:15:46 PM EST'
>>> len(blah.group())
28

I didn't run group to find out the number of characters in a string, I ran
it to find out something about blah and its matches.


>
>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.


No, call it pink, but it all has to do with knowing how many matches in a
re, or the count of tuples from a group.

Just some thoughts leaking out....

Clayton
>
>--
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.flickr.com/photos/alangauldphotos
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list