[Tutor] Tutor Digest, Vol 59, Issue 109

Paul McGuire ptmcg at austin.rr.com
Wed Jan 21 19:45:34 CET 2009


 

-----Original Message-----
From: tutor-bounces+ptmcg=austin.rr.com at python.org
[mailto:tutor-bounces+ptmcg=austin.rr.com at python.org] On Behalf Of
tutor-request at python.org
Sent: Wednesday, January 21, 2009 11:29 AM
To: tutor at python.org
Subject: Tutor Digest, Vol 59, Issue 109

Send Tutor mailing list submissions to
	tutor at python.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
	tutor-request at python.org

You can reach the person managing the list at
	tutor-owner at python.org

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Tutor digest..."


Today's Topics:

   1. The better Python approach (Robert Berman)
   2. Re: The better Python approach (jadrifter)
   3. Re: The better Python approach (Tim Golden)
   4. Re: The better Python approach (Robert Berman)
   5. Re: The better Python approach (Vince Teachout)
   6. Re: The better Python approach (Alan Gauld)
   7. Re: The better Python approach (Andreas Kostyrka)


----------------------------------------------------------------------

Message: 1
Date: Wed, 21 Jan 2009 08:33:50 -0500
From: Robert Berman <bermanrl at cfl.rr.com>
Subject: [Tutor] The better Python approach
To: Tutor at python.org
Message-ID: <4977243E.4010507 at cfl.rr.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Good Morning,

Given a string consisting of numbers separated by spaces such as '1234
5678 1 233 476'. I can see I have two obvious choices to extract or parse
out the numbers. The first relying on iteration so that as I search for a
blank, I build a substring of all characters found before the space and
then, once the space is found, I can then use the int(n) function to
determine the number.  From my C++ background, that is the approach that
seems not only most natural but also most efficient......but....the rules of
Python are different and I easily see that I can also search for the first
blank, then using the character count, I can use the slice operation to get
the characters. Of even further interest I see a string built-in function
called split which, I think, will return all the distinct character sub
strings for me.

My question is what is the most correct python oriented solution for
extracting those substrings?

Thanks,


Robert Berman


------------------------------

Message: 2
Date: Wed, 21 Jan 2009 05:40:00 -0800
From: jadrifter <jadrifter at gmail.com>
Subject: Re: [Tutor] The better Python approach
To: Robert Berman <bermanrl at cfl.rr.com>
Cc: Tutor at python.org
Message-ID: <1232545201.6121.3.camel at ltop>
Content-Type: text/plain

>>>a = '1234 5678 1 233 476'
>>>a.split()
['1234', '5678', '1', '233', '476']

Where the '>>>' are the command prompt from python.  Don't type those.
A space is the default split delimiter.  If you wish to use a '-' or new
line feed them as strings to the split  method.

John

On Wed, 2009-01-21 at 08:33 -0500, Robert Berman wrote:
> Good Morning,
> 
> Given a string consisting of numbers separated by spaces such as '1234
> 5678 1 233 476'. I can see I have two obvious choices to extract or 
> parse out the numbers. The first relying on iteration so that as I 
> search for a blank, I build a substring of all characters found before 
> the space and then, once the space is found, I can then use the int(n) 
> function to determine the number.  From my C++ background, that is the 
> approach that seems not only most natural but also most 
> efficient......but....the rules of Python are different and I easily 
> see that I can also search for the first blank, then using the 
> character count, I can use the slice operation to get the characters. 
> Of even further interest I see a string built-in function called split 
> which, I think, will return all the distinct character sub strings for me.
> 
> My question is what is the most correct python oriented solution for 
> extracting those substrings?
> 
> Thanks,
> 
> 
> Robert Berman
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



------------------------------

Message: 3
Date: Wed, 21 Jan 2009 13:40:15 +0000
From: Tim Golden <mail at timgolden.me.uk>
Subject: Re: [Tutor] The better Python approach
Cc: Tutor at python.org
Message-ID: <497725BF.9060501 at timgolden.me.uk>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Robert Berman wrote:
> Given a string consisting of numbers separated by spaces such as '1234
> 5678 1 233 476'. I can see I have two obvious choices to extract or 
> parse out the numbers. The first relying on iteration so that as I 
> search for a blank, I build a substring of all characters found before 
> the space and then, once the space is found, I can then use the int(n) 
> function to determine the number.  From my C++ background, that is the 
> approach that seems not only most natural but also most 
> efficient......but....the rules of Python are different and I easily 
> see that I can also search for the first blank, then using the 
> character count, I can use the slice operation to get the characters. 
> Of even further interest I see a string built-in function called split 
> which, I think, will return all the distinct character sub strings for me.
> 
> My question is what is the most correct python oriented solution for 
> extracting those substrings?

Correct? I don't know. Working; try this:

<code>
s = '1234 5678 1 233 476'
nums = [int (i) for i in s.split ()]
print nums

</code>

If you had some more sophisticated need (he says, inventing requirements as
he goes along) such as pulling the numbers out of a string of mixed numbers
and letters, you could use a regular
expression:

<code>
import re

s = '1234 abc 5678 *** 1 233 xyz 476'
nums = [int (i) for i in re.findall ("\d+", s)] print nums

</code>

TJG


------------------------------

Message: 4
Date: Wed, 21 Jan 2009 08:50:29 -0500
From: Robert Berman <bermanrl at cfl.rr.com>
Subject: Re: [Tutor] The better Python approach
To: Tutor at python.org
Message-ID: <49772825.5060000 at cfl.rr.com>
Content-Type: text/plain; charset="us-ascii"

An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20090121/a0b41acf/attach
ment-0001.htm>

------------------------------

Message: 5
Date: Wed, 21 Jan 2009 08:59:08 -0500
From: Vince Teachout <teachv at taconic.net>
Subject: Re: [Tutor] The better Python approach
To: Tutor at python.org
Message-ID: <49772A2C.6010107 at taconic.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

jadrifter wrote:
>>>> a = '1234 5678 1 233 476'
>>>> a.split()
> ['1234', '5678', '1', '233', '476']
> 
> Where the '>>>' are the command prompt from python.  Don't type those.
> A space is the default split delimiter.  If you wish to use a '-' or 
> new line feed them as strings to the split  method.
> 

Ok, that's it.  That was the last straw.  I'm digging up my copy of "Dive
into Python" and starting today!


------------------------------

Message: 6
Date: Wed, 21 Jan 2009 14:44:18 -0000
From: "Alan Gauld" <alan.gauld at btinternet.com>
Subject: Re: [Tutor] The better Python approach
To: tutor at python.org
Message-ID: <gl7ccd$5aa$1 at ger.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
	reply-type=original


"Robert Berman" <bermanrl at cfl.rr.com> wrote

> Perhaps i should not have said the most Python correct.
> It looks as if it may well be the approach using the least
> amount of work the interpreter must complete.

That's generally true. Python can always do things the long way but 
its
generally more efficient both in programmer time and performance
speed to use the built-in functions/methods as much as possible.
Most of them are written in C after all!

Alan G 




------------------------------

Message: 7
Date: Wed, 21 Jan 2009 18:29:03 +0100
From: Andreas Kostyrka <andreas at kostyrka.org>
Subject: Re: [Tutor] The better Python approach
To: jadrifter at gmail.com
Cc: Tutor at python.org
Message-ID: <20090121182903.0602ac78 at andi-lap>
Content-Type: text/plain; charset=US-ASCII

Am Wed, 21 Jan 2009 05:40:00 -0800
schrieb jadrifter <jadrifter at gmail.com>:

> >>>a = '1234 5678 1 233 476'
> >>>a.split()
> ['1234', '5678', '1', '233', '476']
[int(x) for x in a.split()] # [1234, 5678, 1, 233, 476]

Andreas


------------------------------

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 59, Issue 109
**************************************



More information about the Tutor mailing list