Python 如何拆分一个复杂的句子字符串,保留分隔符,但不将分隔符放在它应该放的位置';不可能

Python 如何拆分一个复杂的句子字符串,保留分隔符,但不将分隔符放在它应该放的位置';不可能,python,string,Python,String,我在Grepper上看到了以下片段: line = "<html><head>" d = ">" s = [e+d for e in line.split(d) if e] print(s) #Output: #["<html>", "<head>"] line=“” d=“>” s=[e+d表示行中的e。如果e,则拆分(d)] 印刷品 #输出: #["",

我在Grepper上看到了以下片段:

line = "<html><head>"
d = ">"
s =  [e+d for e in line.split(d) if e]
print(s)
#Output:
#["<html>", "<head>"]
line=“”
d=“>”
s=[e+d表示行中的e。如果e,则拆分(d)]
印刷品
#输出:
#["", ""]
对于给定的示例,它工作得很好。但是如果我拆分一个句子,这个片段将添加两次分隔符:

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.:']
line=“”
d=“>”
s=[e+d表示行中的e。如果e,则拆分(d)]
印刷品
#输出:
#['有两种方法:','一种是尝试,另一种是不尝试。']
所以我做了这件事,想出了一个可行的办法:

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
d=“,”
拆分句子=句子。拆分(d)
计数器=0
maxLines=len(拆分句子)
对于拆分句子中的拆分句子:
如果计数器<最大线-1:
段落列表。追加(拆分句子+d)
其他:
段落列表。追加(拆分句子)
计数器=计数器+1
我想知道是否有一种更优雅的方式来做到这一点

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]
依我看,这比任何一行解决方案都更具可读性。

试试这个:

s = line.replace("><", ">#<").split("#")

s=line.replace(">#请从中重复和。请使用可复制的示例明确您想要的内容。第二个示例的输入错误。什么决定了您何时这样做,何时不想将分隔符附加到输出中?您只是想在分隔符处断开字符串吗?这看起来像一个
索引
和切片序列,而不是
split
。我将重复这个过程。是的……在按下post按钮之前,我需要更仔细地检查。是的,我正在尝试在':“不需要在第二个字符串的末尾再次添加它。索引片段可以工作。我会尝试。我还给出了一个答案:只要在适当的时候将最后一个字符串混在一起就行了。很高兴听到它匹配。