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


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
  Class test : good way to split methods into several files paul18fr 4 488 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  [split] Pipenv mohammadasadi4 0 308 Jan-15-2024, 10:35 AM
Last Post: mohammadasadi4
  [split] Why is there an output of None akbarza 1 468 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  [split] Class takes no arguments bily071 2 649 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  [split] Issue installing selenium Akshat_Vashisht 1 551 Oct-18-2023, 02:08 PM
Last Post: Larz60+
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,153 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 1,409 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  [split] How to resolve version conflicts in Python? atonalwilson 1 1,005 May-04-2023, 09:02 AM
Last Post: buran
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,552 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  [split] Parse Nested JSON String in Python mmm07 4 1,547 Mar-28-2023, 06:07 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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