s.split() on multiple separators

mrkafk at gmail.com mrkafk at gmail.com
Sun Sep 30 09:53:14 EDT 2007


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.




More information about the Python-list mailing list