Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Inconsistent behavior of split on empty string

Reply
Thread Tools

Inconsistent behavior of split on empty string

 
 
bk_kl@gmx.de
Guest
Posts: n/a
 
      02-13-2004
Hi,

I think the following behavior of the build in function 'split' is inconsistent.
What do you think?

>>> "".split()

[]
>>> "".split(";")

['']

I'm using python 2.3.3 on Windows 2000.
(Perl's split only returns all items up to the last non-empty item,
e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).
 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      02-17-2004
On 13 Feb 2004 00:46:00 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) ((E-Mail Removed)) wrote:

>Hi,
>
>I think the following behavior of the build in function 'split' is inconsistent.
>What do you think?
>
>>>> "".split()

>[]
>>>> "".split(";")

>['']
>
>I'm using python 2.3.3 on Windows 2000.
>(Perl's split only returns all items up to the last non-empty item,
> e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).


..split() is not an information-preserving split. It is a convenient special case
whose function should not be confused with that of .split(something).

The latter would be buggy if you could not successfully do this:

>>> anystring = ''
>>> somestring = ';'
>>> assert anystring == somestring.join(anystring.split(somestring))
>>> anystring == somestring.join(anystring.split(somestring))

True

Unfortunately (other than making a likely bug announce itself),

>>> somestring = ''
>>> anystring == somestring.join(anystring.split(somestring))

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: empty separator

but otherwise I think it works.

Regards,
Bengt Richter
 
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
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
Inconsistent Behavior Converting String to Integer/Float Daniel Harple Ruby 5 02-27-2006 12:22 AM
java String split returns an additional first empty string Hanif Java 6 10-17-2003 06:21 AM



Advertisments