Velocity Reviews > Re: Stupid string.split question

# Re: Stupid string.split question

Andrew Dalke
Guest
Posts: n/a

 08-06-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

Andrew Dalke
Guest
Posts: n/a

 08-07-2003
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
(E-Mail Removed)

Brian Kelley
Guest
Posts: n/a

 08-07-2003
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*

Duncan Booth
Guest
Posts: n/a

 08-07-2003
Brian Kelley <(E-Mail Removed)> wrote in
news:3f326199\$0\$3951\$(E-Mail Removed):

> 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 (E-Mail Removed)
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?