Python line breaks

Is there a way to split a string in Python using multiple delimiters instead of one? split seems to use only one parameter as a separator.

Also, I cannot import the re module. (This is the main stumbling block).

Any suggestions on how I should do this?

Thanks!

+4
source share
2 answers

To split into multiple sequences, you can simply replace all the sequences that you want to split, with just one sequence, and then split into one sequence.

So,

 s = s.replace("z", "s") s.split("s") 

Divided into s and z.

+9
source

General approach for a list of splitters, please can anyone write this with less code?

Initializing vars:

 >>> splits = ['.', '-', ':', ','] >>> s='hola, que: tal. be' 

Cutting:

 >>> 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'] 
+1
source

Source: https://habr.com/ru/post/1442843/


All Articles