Is there a way to split a string in Python using multiple delimiters instead of one? split
seems to take in only one parameter as delimiter.
Also, I cannot import the re
module. (This is the main stumbling block really.)
Any suggestions on how I should do it?
Thanks!
In order to split on multiple sequences you could simply replace all of the sequences you need to split on with just one sequence and then split on that one sequence.
So
s = s.replace("z", "s")
s.split("s")
Will split on s and z.
Generic approach for a list of splitters, please, someone can write this with less code?
Initializing vars:
>>> splits = ['.', '-', ':', ',']
>>> s='hola, que: tal. be'
Splitting:
>>> r = [ s ]
>>> for p in splits:
... r = reduce(lambda x,y: x+y, map(lambda z: z.split(p), r ))
Results:
>>> r
['hola', ' que', ' tal', ' be']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With