Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > split question

Reply
Thread Tools

split question

 
 
alexk
Guest
Posts: n/a
 
      04-28-2005
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

 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      04-28-2005
On 2005-04-28, alexk <(E-Mail Removed)> 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!!
 
Reply With Quote
 
 
 
 
Michael Spencer
Guest
Posts: n/a
 
      04-28-2005
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
 
Reply With Quote
 
alexk
Guest
Posts: n/a
 
      04-28-2005
Yes, all of you are right. Thank you all for your answers - I'll use a
regex.

 
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
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments