Re: Stupid string.split question

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

  1. Andrew Dalke

    Andrew Dalke Guest

    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
    #1
    1. Advertising

  2. Andrew Dalke

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Aug 7, 2003
    #2
    1. Advertising

  3. Andrew Dalke

    Brian Kelley Guest

    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*
     
    Brian Kelley, Aug 7, 2003
    #3
  4. Andrew Dalke

    Duncan Booth Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos Ribeiro
    Replies:
    11
    Views:
    736
    Alex Martelli
    Sep 17, 2004
  2. Brandon McCombs
    Replies:
    4
    Views:
    535
    Richard Wheeldon
    Aug 28, 2006
  3. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,057
  4. Sam Kong
    Replies:
    5
    Views:
    276
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    707
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page