[Tutor] The better Python approach
jadrifter at gmail.com
Wed Jan 21 14:40:00 CET 2009
>>>a = '1234 5678 1 233 476'
['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.
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?
> Robert Berman
> Tutor maillist - Tutor at python.org
More information about the Tutor