Posts: 4,597
Threads: 1,479
Joined: Sep 2016
Aug-04-2023, 02:54 AM
(This post was last modified: Aug-04-2023, 02:54 AM by Skaperen.)
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?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 6,314
Threads: 16
Joined: Feb 2020
Posts: 880
Threads: 134
Joined: Jul 2017
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
Posts: 989
Threads: 104
Joined: Sep 2019
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']
Posts: 7,134
Threads: 122
Joined: Sep 2016
(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']
Posts: 880
Threads: 134
Joined: Jul 2017
Oh yes, but then you have imported re to do the work, which is, possibly, somewhat longer than 1 line!
Posts: 4,567
Threads: 71
Joined: Jan 2018
(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)
Posts: 6,314
Threads: 16
Joined: Feb 2020
And you used a module. Pedroski55 prefers not using any modules and longs for a way to directly enter the python bytecodes.
Posts: 7,134
Threads: 122
Joined: Sep 2016
(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()
Posts: 880
Threads: 134
Joined: Jul 2017
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()
|