How to split a complex sentence string, keeping the delimiter, but not also placing the demiiter where it shouldn’t be

Tags: ,



I came across this snippet on Grepper:

line = "<html><head>"
d = ">"
s =  [e+d for e in line.split(d) if e]
print(s)
#Output:
#["<html>", "<head>"]

It works fine for the example given. But if I split a sentence, this snippet will add the delimiter twice:

line = "<html><head>"
d = ">"
s =  [e+d for e in line.split(d) if e]
print(s)
#Output:
#['There are two methods:', ' one is to try, the other is to not try.:']

so I worked on it and came up with this, which works:

d = ","
splitSentences = sentence.split(d)
counter = 0
maxLines = len(splitSentences)
for splitSentence in splitSentences:
    if counter < maxLines - 1:
       paragraphList.append(splitSentence + d)
    else:
       paragraphList.append(splitSentence)
    counter = counter + 1

I’m wondering if there is a way to do this in more elegant way.

Answer

s =  [e+d for e in line.split(d) if e]
# If the delimiter is not the last character, then drop it from the last string.
if line[-1] != d:
    s[-1] = s[-1][:-1]

IMO, This is more readable than any one-line solution.



Source: stackoverflow