469,917 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Behaviour of str.split

Hi,

I'm curious about the behaviour of the str.split() when applied to empty
strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.

I would have expected the second example to have also returned an empty
list. What am I missing?
TIA,

Will McGugan
--
http://www.willmcgugan.com
"".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-84)%26) for c
in "jvyy*jvyyzpthtna^pbz" ] )
Jul 19 '05 #1
9 4132
The behaviour of "".split("*") is not that strange as the splitpoint
always disappear. The re.split() have a nice option to keep the
splitpoint which the str.split should have, I think.

One expectation I keep fighting within myself is that I expect

"mystring".split('') to return ['m', 'y', 's', 't', 'r', 'i', 'n',
'g']. But I guess it's in line with "There should be one-- and
preferably only one --obvious way to do it." that it's not so.

Jul 19 '05 #2

runes wrote:
The behaviour of "".split("*") is not that strange as the splitpoint
always disappear. The re.split() have a nice option to keep the
splitpoint which the str.split should have, I think.

One expectation I keep fighting within myself is that I expect

"mystring".split('') to return ['m', 'y', 's', 't', 'r', 'i', 'n',
'g']. But I guess it's in line with "There should be one-- and
preferably only one --obvious way to do it." that it's not so.


Fortunately, this is easy to write as: list("mystring").
Actually for me it's not so counter-intuitive that "mystring".split('')
doesn't work; what are you trying to split on?

Anyways, I usually need to split on something more complicated so I
split with regexes, usually.

cheers,

--Tim

Jul 19 '05 #3
[Tim N. van der Leeuw]
Fortunately, this is easy to write as: list("mystring").


Sure, and map(None, "mystring")

Anyways, I have settled with this bevaviour, more or less ;-)

Rune

Jul 19 '05 #4
On Mon, 18 Apr 2005 16:16:00 +0100, Will McGugan
<ne**@NOwillmcguganSPAM.com> wrote:
Hi,

I'm curious about the behaviour of the str.split() when applied to empty
strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.

I would have expected the second example to have also returned an empty
list. What am I missing?


You are missing a perusal of the documentation. Had you done so, you
would have noticed that the actual behaviour that you mentioned is
completely the reverse of what is in the documentation!

"""
Splitting an empty string with a specified separator returns an empty
list.
If sep is not specified or is None, a different splitting algorithm is
applied. <snip> Splitting an empty string or a string consisting of
just whitespace will return "['']".
"""

As you stumbled on this first, you may have the honour of submitting a
patch to the docs and getting your name on the roll of contributors.
Get in quickly, before X** L** does :-)

Cheers,

John
Jul 19 '05 #5
[Will McGugan]
I'm curious about the behaviour of the str.split() when applied to empty
strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.

[John Machin] You are missing a perusal of the documentation. Had you done so, you
would have noticed that the actual behaviour that you mentioned is
completely the reverse of what is in the documentation!


Nuts! I've got it from here and will get it fixed up.

<lament>
str.split() has to be one of the most frequently revised pieces of
documentation. In this case, the two statements about splitting empty strings
need to be swapped. Previously, all the changes occured because
someone/somewhere would always find a way to misread whatever was there.

In the absence of reading the docs, a person's intuition seems to lead them to
guess that the behavior will be different than it actually is. Unfortunately,
one person's intuition is often at odds with another's.
</lament>
Raymond Hettinger
Jul 19 '05 #6
Will McGugan wrote:
Hi,

I'm curious about the behaviour of the str.split() when applied to empty
strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.


Both of these make sense as limiting cases.

Consider
"a b c".split() ['a', 'b', 'c'] "a b".split() ['a', 'b'] "a".split() ['a'] "".split() []

and
"**".split("*") ['', '', ''] "*".split("*") ['', ''] "".split("*")

['']

The split() method is really doing two somewhat different things
depending on whether it is given an argument, and the end-cases
come out differently.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #7
Greg Ewing wrote:
Will McGugan wrote:
Hi,

I'm curious about the behaviour of the str.split() when applied to
empty strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.

Both of these make sense as limiting cases.

Consider
>>> "a b c".split() ['a', 'b', 'c'] >>> "a b".split() ['a', 'b'] >>> "a".split() ['a'] >>> "".split() []

and
>>> "**".split("*") ['', '', ''] >>> "*".split("*") ['', ''] >>> "".split("*") ['']

The split() method is really doing two somewhat different things
depending on whether it is given an argument, and the end-cases
come out differently.

You don't really explain *why* they make sense as limiting cases, as
your examples are quite different.

Consider
"a*b*c".split("*") ['a', 'b', 'c'] "a*b".split("*") ['a', 'b'] "a".split("*") ['a'] "".split("*")

['']

Now how is this logical when compared with split() above?

David
Jul 19 '05 #8
On Wed, 20 Apr 2005 10:55:18 +0200, David Fraser <da****@sjsoft.com> wrote:
Greg Ewing wrote:
Will McGugan wrote:
Hi,

I'm curious about the behaviour of the str.split() when applied to
empty strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.

Both of these make sense as limiting cases.

Consider
>>> "a b c".split()

['a', 'b', 'c']
>>> "a b".split()

['a', 'b']
>>> "a".split()

['a']
>>> "".split()

[]

and
>>> "**".split("*")

['', '', '']
>>> "*".split("*")

['', '']
>>> "".split("*")

['']

The split() method is really doing two somewhat different things
depending on whether it is given an argument, and the end-cases
come out differently.

You don't really explain *why* they make sense as limiting cases, as
your examples are quite different.

Consider
"a*b*c".split("*")['a', 'b', 'c'] "a*b".split("*")['a', 'b'] "a".split("*")['a'] "".split("*")['']

Now how is this logical when compared with split() above?


The trouble is that s.split(arg) and s.split() are two different functions.

The first is 1:1 and reversible like arg.join(s.split(arg))==s
The second is not 1:1 nor reversible: '<<various whitespace>>'.join(s.split()) == s ?? Not usually.

I think you can do it with the equivalent whitespace regex, preserving the splitout whitespace
substrings and ''.joining those back with the others, but not with split(). I.e.,
def splitjoin(s, splitter=None): ... return (splitter is None and '<<whitespace>>' or splitter).join(s.split(splitter))
... splitjoin('a*b*c', '*') 'a*b*c' splitjoin('a*b', '*') 'a*b' splitjoin('a', '*') 'a' splitjoin('', '*') '' splitjoin('a b c') 'a<<whitespace>>b<<whitespace>>c' splitjoin('a b ') 'a<<whitespace>>b' splitjoin(' b ') 'b' splitjoin('') ''
splitjoin('*****','*') '*****'
Note why that works:
'*****'.split('*') ['', '', '', '', '', ''] '*a'.split('*') ['', 'a'] 'a*'.split('*') ['a', '']
splitjoin('*a','*') '*a' splitjoin('a*','*')

'a*'

Regards,
Bengt Richter
Jul 19 '05 #9
Bengt Richter wrote:
On Wed, 20 Apr 2005 10:55:18 +0200, David Fraser <da****@sjsoft.com> wrote:

Greg Ewing wrote:
Will McGugan wrote:
Hi,

I'm curious about the behaviour of the str.split() when applied to
empty strings.

"".split() returns an empty list, however..

"".split("*") returns a list containing one empty string.
Both of these make sense as limiting cases.

Consider

>>> "a b c".split()
['a', 'b', 'c']
>>> "a b".split()
['a', 'b']
>>> "a".split()
['a']
>>> "".split()
[]

and

>>> "**".split("*")
['', '', '']
>>> "*".split("*")
['', '']
>>> "".split("*")
['']

The split() method is really doing two somewhat different things
depending on whether it is given an argument, and the end-cases
come out differently.


You don't really explain *why* they make sense as limiting cases, as
your examples are quite different.

Consider
>"a*b*c".split("*")


['a', 'b', 'c']
>"a*b".split("*")


['a', 'b']
>"a".split("*")


['a']
>"".split("*")


['']

Now how is this logical when compared with split() above?

The trouble is that s.split(arg) and s.split() are two different functions.

The first is 1:1 and reversible like arg.join(s.split(arg))==s
The second is not 1:1 nor reversible: '<<various whitespace>>'.join(s.split()) == s ?? Not usually.

I think you can do it with the equivalent whitespace regex, preserving the splitout whitespace
substrings and ''.joining those back with the others, but not with split(). I.e.,
>>> def splitjoin(s, splitter=None): ... return (splitter is None and '<<whitespace>>' or splitter).join(s.split(splitter))
... >>> splitjoin('a*b*c', '*') 'a*b*c' >>> splitjoin('a*b', '*') 'a*b' >>> splitjoin('a', '*') 'a' >>> splitjoin('', '*') '' >>> splitjoin('a b c') 'a<<whitespace>>b<<whitespace>>c' >>> splitjoin('a b ') 'a<<whitespace>>b' >>> splitjoin(' b ') 'b' >>> splitjoin('') ''
>>> splitjoin('*****','*') '*****'
Note why that works:
>>> '*****'.split('*') ['', '', '', '', '', ''] >>> '*a'.split('*') ['', 'a'] >>> 'a*'.split('*') ['a', '']
>>> splitjoin('*a','*') '*a' >>> splitjoin('a*','*')

'a*'


Thanks, this makes sense.
So ideally if we weren't dealing with backward compatibility these
functions might have different names... "split" (with arg) and
"spacesplit" (without arg)
In fact it would be nice to allow an argument to "spacesplit" specifying
the characters regarded as 'space'
But all not worth breaking current code :-)

David
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Timothy Madden | last post: by
4 posts views Thread by bill | last post: by
4 posts views Thread by Tim Streater | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
2 posts views Thread by Adam Honek | last post: by
7 posts views Thread by Andrew McLean | last post: by
3 posts views Thread by Walter Cruz | last post: by
5 posts views Thread by Steve | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.