Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie question regarding string.split()

Reply
Thread Tools

Newbie question regarding string.split()

 
 
kevinliu23
Guest
Posts: n/a
 
      04-20-2007
Hey guys,

So I have a question regarding the split() function in the string
module. Let's say I have an string...

input = "2b 3 4bx 5b 2c 4a 5a 6"
projectOptions = (input.replace(" ", "")).split('2')
print projectOptions

['', 'b34bx5b', 'c4a5a6']

My question is, why is the first element of projectOptions an empty
string? What can I do so that the first element is not an empty
string? but the 'b34bx5b' string as I expected?

Thanks so much guys.

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      04-20-2007
On 2007-04-20, kevinliu23 <> wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']
>
> My question is, why is the first element of projectOptions an
> empty string?


The presense of a delimiter indicates that there is a field
both before and after the delimiter. If it didn't work that
way, then you'd get the same results for

input = "2b 3 4bx 5b 2c 4a 5a 6"

as you would for

input = "b 3 4bx 5b 2c 4a 5a 6"

you would get the same results for

input = "2222b22222"

as you would for

intput = "b"

> What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?


projectOptions = (input.replace(" ", "")).split('2')
if projectOptions[0] == '':
del projectOptions[0]
print projectOptions

--
Grant Edwards grante Yow! I feel like a wet
at parking meter on Darvon!
visi.com
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      04-20-2007
kevinliu23 wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>

First of all, the string module is pretty much deprecated nowadays. What
you are actually using, the .split() method of a string, is the
preferred way to do it. If you are importing string, don't bother!


> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']
>
> My question is, why is the first element of projectOptions an empty
> string? What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?
>

Because .split() returns a list of the strings surrounding each
occurrence of the split argument. Because the string begins with the
split argument it returns an empty string as the first element (since
the assumption is you are interested in both sides of the separator).

You can easily throw the first element away:

del projectOptions [0]

for example, or

projectOptions = projectOptions[1:]

But what do you want to do if the string *doesn't* begin with a 2?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      04-20-2007
On Apr 20, 1:51 pm, kevinliu23 <kevinli...@gmail.com> wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']
>
> My question is, why is the first element of projectOptions an empty
> string? What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?
>
> Thanks so much guys.


The reason you have an empty string at the beginning is because you
are "splitting" on a character that happens to include the first
character in your string. So what you are telling Python to do is to
split the beginning from itself, or to insert a blank so that it is
split.

Also, you shouldn't use "input" as a variable name since it is a built-
in variable.

One hack to make it work is to add the following line right before you
print "projectOptions":

projectOptions.pop(0) # pop the first element out of the list



Mike


 
Reply With Quote
 
Stephen Lewitowski
Guest
Posts: n/a
 
      04-20-2007
kevinliu23 wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']
>
> My question is, why is the first element of projectOptions an empty
> string? What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?
>
> Thanks so much guys.
>

split on c instead
 
Reply With Quote
 
Tommy Grav
Guest
Posts: n/a
 
      04-20-2007

On Apr 20, 2007, at 3:15 PM, wrote:
> On Apr 20, 1:51 pm, kevinliu23 <kevinli...@gmail.com> wrote:
>> ['', 'b34bx5b', 'c4a5a6']
>>
>> My question is, why is the first element of projectOptions an empty
>> string? What can I do so that the first element is not an empty
>> string? but the 'b34bx5b' string as I expected?
>>
>> Thanks so much guys.

>
> The reason you have an empty string at the beginning is because you
> are "splitting" on a character that happens to include the first
> character in your string. So what you are telling Python to do is to
> split the beginning from itself, or to insert a blank so that it is
> split.


So why does this not happen when you use the empty split() function?

[tgrav@Thrym] /Users/tgrav --> python
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = " 456 556 556"
>>> a.split()

['456', '556', '556']
>>> a.split(" ")

['', '456', '556', '556']
>>>


What exactly does .split() use to do the splitting?

Cheers
Tommy
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      04-20-2007
Tommy Grav wrote:
> On Apr 20, 2007, at 3:15 PM, wrote:
>> On Apr 20, 1:51 pm, kevinliu23 <kevinli...@gmail.com> wrote:
>>> ['', 'b34bx5b', 'c4a5a6']
>>>
>>> My question is, why is the first element of projectOptions an empty
>>> string? What can I do so that the first element is not an empty
>>> string? but the 'b34bx5b' string as I expected?
>>>
>>> Thanks so much guys.

>> The reason you have an empty string at the beginning is because you
>> are "splitting" on a character that happens to include the first
>> character in your string. So what you are telling Python to do is to
>> split the beginning from itself, or to insert a blank so that it is
>> split.

>
> So why does this not happen when you use the empty split() function?
>
> [tgrav@Thrym] /Users/tgrav --> python
> Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = " 456 556 556"
> >>> a.split()

> ['456', '556', '556']
> >>> a.split(" ")

> ['', '456', '556', '556']
> >>>

>
> What exactly does .split() use to do the splitting?
>

Any sequence of one or more whitespace characters. This is a rather
special case, quite different from .split(" ").

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-21-2007
On Fri, 20 Apr 2007 12:15:33 -0700, kyosohma wrote:

> One hack to make it work is to add the following line right before you
> print "projectOptions":
>
> projectOptions.pop(0) # pop the first element out of the list


Which will introduce a nice bug into the Original Poster's code when the
input string doesn't start with a "2".



--
Steven.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-21-2007
kevinliu23 a écrit :
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')

The parens around the call to input.replace are useless:
projectOptions = input.replace(" ", "").split('2')

> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']


(snip)

> What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?



projectOptions = filter(None, input.replace(" ", "").split('2'))


 
Reply With Quote
 
kevinliu23
Guest
Posts: n/a
 
      04-21-2007
On Apr 21, 3:30 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.fr> wrote:
> kevinliu23 a écrit :> Hey guys,
>
> > So I have a question regarding the split() function in the string
> > module. Let's say I have an string...

>
> > input = "2b 3 4bx 5b 2c 4a 5a 6"
> > projectOptions = (input.replace(" ", "")).split('2')

Thanks for all your help everyone.

> The parens around the call to input.replace are useless:
> projectOptions = input.replace(" ", "").split('2')
>
> > print projectOptions

>
> > ['', 'b34bx5b', 'c4a5a6']

>
> (snip)
>
> > What can I do so that the first element is not an empty
> > string? but the 'b34bx5b' string as I expected?

>
> projectOptions = filter(None, input.replace(" ", "").split('2'))



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Complete newbie question regarding light! Steve Tanner Digital Photography 5 12-28-2003 04:17 PM
Newbie - Question regarding photo printing Rich Digital Photography 4 11-24-2003 01:52 PM
newbie question regarding camcorder the niner nation Digital Photography 1 11-05-2003 10:36 PM
newbie question regarding using c with c++ radiohead C++ 5 09-26-2003 11:40 AM
Newbie question regarding multiple DVD's of VHS tapes nan DVD Video 4 09-24-2003 11:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57