Python Forum
doing string split with 2 or more split characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
doing string split with 2 or more split characters
#1
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.
Reply
#2
re.split()?
Reply
#3
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
Reply
#4
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']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#5
(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']
Reply
#6
Oh yes, but then you have imported re to do the work, which is, possibly, somewhat longer than 1 line!
Reply
#7
(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)
Reply
#8
And you used a module. Pedroski55 prefers not using any modules and longs for a way to directly enter the python bytecodes.
Reply
#9
(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()
Reply
#10
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] How to ask Smart Questions (thread title expansion) yoanselcp 2 726 Dec-25-2025, 07:58 AM
Last Post: Pedroski55
  [solved] re.split issue paul18fr 10 1,086 Nov-14-2025, 06:02 PM
Last Post: deanhystad
  [split] print two different sequence number Reema 1 685 Nov-10-2025, 05:37 AM
Last Post: Gribouillis
  [split] How to continue code after .show() in matplotlib? pythonnewbie62 1 840 Apr-28-2025, 04:02 PM
Last Post: deanhystad
  [split] ibm_db install problem SQLPython 1 2,417 Feb-13-2025, 07:24 PM
Last Post: buran
  [split] Newbie needs help Schoe1 0 847 Feb-12-2025, 06:57 PM
Last Post: Schoe1
Star how to split pdf under 10mb using python skchui9786 4 3,176 Jan-18-2025, 03:25 AM
Last Post: skchui9786
  [split] another problem with code blakeusherremix68 0 888 Dec-23-2024, 11:36 PM
Last Post: blakeusherremix68
  [split] Code help emma1423 1 1,154 Dec-13-2024, 02:00 PM
Last Post: perfringo
  [split] Prime numbers saima 1 1,028 Dec-09-2024, 02:19 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020