# Re: Stupid string.split question

Discussion in 'Python' started by Andrew Dalke, Aug 6, 2003.

1. ### Andrew DalkeGuest

Brian Kelley:
> "ABCDEF".split("") shouldn't equal ["A", "B", "C", "D", "E", "F"]?

(which, btw, raises a ValueError.)

Hmm. Perl does that, right? I see your point about the symmetry,

> It seems that if you can join with an empty seperator, you should be
> able to split with one.

Even better, newer splits take a substring to split on, as in

>>> "ABCDEFG".split("BCD")

['A', 'EFG']
>>> "ABCDEFG".split("DCE")

['ABCDEFG']
>>>

arguing for a closer similarity.

> I suppose the pythonic way is [x for x in "ABCDEF"] which doesn't make
> as much sense to be.

list("ABCDEF")

[to reverse a string]
> l = [x for x in forward_string]
> l.reverse()
> reversed_string = "".join(l)

or in Python 2.3
>>> "ABCDEF"[::-1]

'FEDCBA'
>>>

Andrew

Andrew Dalke, Aug 6, 2003

2. ### Andrew DalkeGuest

Peter Hansen:
> It's only a token kind of symmetry, without much real value. After
> all, why shouldn't this be symmetrical, too, then? :
>
> ''.join(['abc', 'def', 'g', 'h']) --> 'abcdefgh'
>
> 'abcdefgh'.split('') --> ['abc', 'def', 'g', 'h']

That's a different symmetry than what Brian is arguing.
He doesn't expect

Instead, he is bothered because the following doesn't work
when sep == ""

def split_join(s, sep):
return s == sep.join(s.split(sep)):

Andrew

Andrew Dalke, Aug 7, 2003

3. ### Brian KelleyGuest

Andrew Dalke wrote:

> Instead, he is bothered because the following doesn't work
> when sep == ""
>
> def split_join(s, sep):
> return s == sep.join(s.split(sep)):
>

That's pretty much it. Note that I'm not bothered so much to actually

I understand the infinite number of empty seperators though, and while
it might give pause I wonder (as an academic excercise) if you asked 100
people what "ABCDEF".split('') should return (if it returned anything at
all) what you would get. Again, don't bother answering this question,
list("ABCDEF") works just fine for me

Now, as for old chesnuts and long hot summers, if one could just do:

"".join(list("ABCDEF").reverse())

*ducks*

Brian Kelley, Aug 7, 2003
4. ### Duncan BoothGuest

Brian Kelley <> wrote in
news:3f326199\$0\$3951\$:

> Now, as for old chesnuts and long hot summers, if one could just do:
>
> "".join(list("ABCDEF").reverse())
>
> *ducks*
>

In what way would that be an improvement on writing

"ABCDEF"[::-1]

? *ducks and runs*

--
Duncan Booth
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Duncan Booth, Aug 7, 2003