s.split() on multiple separators

William James w_a_x_man at yahoo.com
Sun Sep 30 14:27:32 EDT 2007


On Sep 30, 8:53 am, mrk... at gmail.com wrote:
> Hello everyone,
>
> OK, so I want to split a string c into words using several different
> separators from a list (dels).
>
> I can do this the following C-like way:
>
> >>> c=' abcde abc cba fdsa bcd '.split()
> >>> dels='ce '
> >>> for j in dels:
>
>         cp=[]
>         for i in xrange(0,len(c)-1):
>                 cp.extend(c[i].split(j))
>         c=cp
>
> >>> c
>
> ['ab', 'd', '', 'ab', '', '']
>
> But. Surely there is a more Pythonic way to do this?
>
> I cannot do this:
>
> >>> for i in dels:
>
>         c=[x.split(i) for x in c]
>
> because x.split(i) is a list.

E:\Ruby>irb
irb(main):001:0> ' abcde abc cba fdsa bcd '.split(/[ce ]/)
=> ["", "ab", "d", "", "ab", "", "", "ba", "fdsa", "b", "d"]




More information about the Python-list mailing list