Python Forum

Full Version: doing string split with 2 or more split characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
if i have the string 'ab|cd!ef|gh!ij|kl!mn' and want to split it into ['ab','cd','ef','gh','ij','kl','mn'], is there a better way than just replacing all the splitter characters to be the same? sometimes i get too many .replace() calls. suggestions to get this nice, pythonic, and in one line?
re.split()?
Can't see a one-liner to do this.

# add a unwanted character at the end of the string or you won't get the last wanted characters
mystring = 'ab|cd!ef|gh!ij|kl!mn|'
# define what you want to keep
wanted = 'abcdefghijklmnopqrstuvwxyz'
count = 0
for i in range(len(mystring)):    
    if not mystring[i] in wanted:
        seq = mystring[count:i]
        print(seq)
        count = i+1
As deanhystad suggested
import re
string = 'ab|cd!ef|gh!ij|kl!mn'
print(re.split('[\|!]', string))
Output:
['ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn']
(Aug-04-2023, 06:44 AM)Pedroski55 Wrote: [ -> ]Can't see a one-liner to do this
>>> s = 'ab|cd!ef|gh!ij|kl!mn'
>>> ''.join(c if not c in '|!' else ' ' for c in s).split()
['ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn']
With regex can just use \W(matches any non-word character)
>>> import re
>>> 
>>> s = 'ab|cd!ef|gh!ij|kl!mn'
>>> re.split(r'\W', s)
['ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn']
Oh yes, but then you have imported re to do the work, which is, possibly, somewhat longer than 1 line!
(Aug-04-2023, 03:27 PM)Pedroski55 Wrote: [ -> ]but then you have imported re to do the work, which is, possibly, somewhat longer than 1 line!
__import__('re').split(r'\W', s)
And you used a module. Pedroski55 prefers not using any modules and longs for a way to directly enter the python bytecodes.
(Aug-04-2023, 03:27 PM)Pedroski55 Wrote: [ -> ]Oh yes, but then you have imported re to do the work, which is, possibly, somewhat longer than 1 line!
Did you not 👀 the first one.
''.join(c if not c in '|!' else ' ' for c in s).split()
No, sorry, didn't see that! Very good, I like it!

Didn't know you can put so much in ''.join()!!

# original string
s = 'ab|cd!ef|gh!ij|kl!mn'
# add anything you want to keep
wanted = 'abcdefghijklmnopqrstuvwxyz'
# things you don't want
unwanted = set([s[i] for i in range(len(s)) if not s[i] in wanted])      
# from snippsat I like this
''.join(c if not c in unwanted else ' ' for c in s).split()
Pages: 1 2 3