Home > OS >  How to split sentence in a list based on two words?
How to split sentence in a list based on two words?

Time:01-12

I have a list of string like this lst = ['John Kim and Kerry Lin', 'John Cena', 'Kim Rai with Kaster Baldwin'], and I would like to split the words in list if they have and or with as separators such that the final outcome is ['John Kim', 'Kerry Lin', 'John Cena', 'Kim Rai', 'Kaster Baldwin']. How do I achieve this? My try was:

to_ret = []
for words in lst:
 splitted = words.split(' and')
 to_ret.extend(splitted)
new_ret = []
for words in to_ret:
 splitted = words.split(' with')
 new_ret.extend(splitted)

but this looks very repetitive. Any suggestions for cleaner code?

CodePudding user response:

You could use regular expressions to handle the multiple delimiters, and chain to put all of the sublists into one.

import re
from itertools import chain
lst = ['John Kim and Kerry Lin', 'John Cena', 'Kim Rai with Kaster Baldwin']

output = [w.strip() for w in chain.from_iterable([re.split(r'and|with',x) for x in lst])]
print(output)

Output

['John Kim', 'Kerry Lin', 'John Cena', 'Kim Rai', 'Kaster Baldwin']

CodePudding user response:

#my interpretation would be
lst = ['John Kim and Kerry Lin', 'John Cena', 'Kim Rai with Kaster Baldwin']


toSplitWith= ["and" , "with"]

ans=[]
for word in lst:
    for sprt in toSplitWith:
        if sprt in word:
            ans.extend(word.split(sprt))

print(ans)

CodePudding user response:

you can do like this if you like:

lst = ['John Kim and Kerry Lin', 'John Cena', 'Kim Rai with Kaster Baldwin']
to_ret = []
for iter_words in range(lst.__len__()):
 splittedand = lst[iter_words].split(' and')
 splittedwith = lst[iter_words].split(' with')
 to_ret.extend(splittedand)
 to_ret.extend(splittedwith)
 to_ret.remove(lst[iter_words])
print(to_ret)
  •  Tags:  
  • Related