Re: Behaviour of str.split
Bengt Richter wrote: On Wed, 20 Apr 2005 10:55:18 +0200, David Fraser <[EMAIL PROTECTED]> 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: '<>'.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 '<>' or splitter).join(s.split(splitter)) ... >>> splitjoin('a*b*c', '*') 'a*b*c' >>> splitjoin('a*b', '*') 'a*b' >>> splitjoin('a', '*') 'a' >>> splitjoin('', '*') '' >>> splitjoin('a bc') 'a<>b<>c' >>> splitjoin('a b') 'a<>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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
On Wed, 20 Apr 2005 10:55:18 +0200, David Fraser <[EMAIL PROTECTED]> 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: '<>'.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 '<>' or splitter).join(s.split(splitter)) ... >>> splitjoin('a*b*c', '*') 'a*b*c' >>> splitjoin('a*b', '*') 'a*b' >>> splitjoin('a', '*') 'a' >>> splitjoin('', '*') '' >>> splitjoin('a bc') 'a<>b<>c' >>> splitjoin('a b') 'a<>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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
[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. 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. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
On Mon, 18 Apr 2005 16:16:00 +0100, Will McGugan <[EMAIL PROTECTED]> 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. 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
[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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of str.split
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. -- http://mail.python.org/mailman/listinfo/python-list
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" ] ) -- http://mail.python.org/mailman/listinfo/python-list