Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Stupid string.split question

Reply
Thread Tools

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



 
Reply With Quote
 
 
 
 
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

"a".join(["abc", "def", "cab"]) --> "abcadefacab"

"abcadefacab".split() --> ["abc", "def", "cab"]

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



 
Reply With Quote
 
 
 
 
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
do anything about it

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*

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      08-07-2003
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid, STUPID question! rincewind HTML 25 05-08-2009 01:07 PM
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57