split question

Discussion in 'Python' started by alexk, Apr 28, 2005.

  1. alexk

    alexk Guest

    I've a simple question. Why the following:

    words = "123#@$#$@^%[wordA] wordB#@$".split('~`!@#$%^&*()_+-=[]{},./')

    doesn't work? The length of the result vector is 1.

    I'm using ActivePython 2.4

    Alex
    alexk, Apr 28, 2005
    #1
    1. Advertising

  2. On 2005-04-28, alexk <> wrote:
    > I've a simple question. Why the following:
    >
    > words = "123#@$#$@^%[wordA] wordB#@$".split('~`!@#$%^&*()_+-=[]{},./')
    >
    > doesn't work?


    But it does work. Your input string (the one on the left) does
    not contain the delimiter string you're passing to the split()
    method. The argument to split() is a delimiter string not a
    set of delimter characters.

    > The length of the result vector is 1.


    Yup :)

    --
    Grant Edwards grante Yow! This MUST be a good
    at party -- My RIB CAGE is
    visi.com being painfully pressed up
    against someone's MARTINI!!
    Grant Edwards, Apr 28, 2005
    #2
    1. Advertising

  3. alexk wrote:
    > I've a simple question. Why the following:
    >
    > words = "123#@$#$@^%[wordA] wordB#@$".split('~`!@#$%^&*()_+-=[]{},./')
    >
    > doesn't work? The length of the result vector is 1.
    >
    > I'm using ActivePython 2.4
    >
    > Alex
    >

    Do you mean, why doesn't it split on every character in '~`!@#$%^&*()_+-=[]{},./' ?

    Help on built-in function split:

    split(...)
    S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the
    delimiter string. If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator.

    sep as a whole is the delimeter string

    If you want to split on any of the characters in your sep string, use a regexp:
    Perhaps:
    >>> import re
    >>> splitter = re.compile("[\[\]~`!@#$%^&*()_+-= ]+") #note escapes for []
    >>> splitter.split("123#@$#$@^%[wordA] wordB#@$")

    ['', 'wordA', 'wordB', '']
    >>>


    is closer to what you had in mind

    Michael
    Michael Spencer, Apr 28, 2005
    #3
  4. alexk

    alexk Guest

    Yes, all of you are right. Thank you all for your answers - I'll use a
    regex.
    alexk, Apr 28, 2005
    #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. Replies:
    2
    Views:
    451
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    684
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    201
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    227
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    582
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page