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

Send Tutor mailing list submissions to
        tutor@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-requ...@python.org

You can reach the person managing the list at
        tutor-ow...@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 <berma...@cfl.rr.com>
Subject: [Tutor] The better Python approach
To: Tutor@python.org
Message-ID: <4977243e.4010...@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 <jadrif...@gmail.com>
Subject: Re: [Tutor] The better Python approach
To: Robert Berman <berma...@cfl.rr.com>
Cc: Tutor@python.org
Message-ID: <1232545201.6121.3.ca...@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@python.org
> http://mail.python.org/mailman/listinfo/tutor



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

Message: 3
Date: Wed, 21 Jan 2009 13:40:15 +0000
From: Tim Golden <m...@timgolden.me.uk>
Subject: Re: [Tutor] The better Python approach
Cc: Tutor@python.org
Message-ID: <497725bf.9060...@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 <berma...@cfl.rr.com>
Subject: Re: [Tutor] The better Python approach
To: Tutor@python.org
Message-ID: <49772825.5060...@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 <tea...@taconic.net>
Subject: Re: [Tutor] The better Python approach
To: Tutor@python.org
Message-ID: <49772a2c.6010...@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.ga...@btinternet.com>
Subject: Re: [Tutor] The better Python approach
To: tutor@python.org
Message-ID: <gl7ccd$5a...@ger.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=original


"Robert Berman" <berma...@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 <andr...@kostyrka.org>
Subject: Re: [Tutor] The better Python approach
To: jadrif...@gmail.com
Cc: Tutor@python.org
Message-ID: <20090121182903.0602a...@andi-lap>
Content-Type: text/plain; charset=US-ASCII

Am Wed, 21 Jan 2009 05:40:00 -0800
schrieb jadrifter <jadrif...@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@python.org
http://mail.python.org/mailman/listinfo/tutor


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

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

Reply via email to