Behaviour of str.split

W

Will McGugan

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
 
R

runes

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.
 
T

Tim N. van der Leeuw

runes said:
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
 
R

runes

[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
 
J

John Machin

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
 
R

Raymond Hettinger

[Will McGugan]
[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
 
G

Greg Ewing

Will said:
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.
 
D

David Fraser

Greg said:
Will said:
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
 
B

Bengt Richter

Greg said:
Will said:
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.,
''
'*****'
Note why that works:
>>> '*****'.split('*') ['', '', '', '', '', '']
>>> '*a'.split('*') ['', 'a']
>>> 'a*'.split('*')
['a', '']
'a*'

Regards,
Bengt Richter
 
D

David Fraser

Bengt said:
Greg said:
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.,
... return (splitter is None and ' said:
'*****'
Note why that works:
'*****'.split('*') ['', '', '', '', '', '']
'*a'.split('*') ['', 'a']
'a*'.split('*')
['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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,235
Messages
2,571,181
Members
47,818
Latest member
KazukoXea6

Latest Threads

Top