使用python将要点句附加到主句

2024-06-01 14:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个段落,格式如下

Lorem Ipsum只是印刷排版行业的虚拟文本; (a) 它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表的发行而流行;最近,像Aldus PageMaker这样的桌面发布软件包括了Lorem Ipsum的版本。 (b) 与大众信仰相反;Lorem Ipsum不是简单的随机文本

为此,我需要收集要点(a)和(b),并将其附加到主要部分,如下面的内容, Lorem Ipsum只是印刷排版行业的虚拟文本。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表的发行而流行;最近,像Aldus PageMaker这样的桌面发布软件包括了Lorem Ipsum的版本

以及

Lorem Ipsum只是印刷排版行业的虚拟文本,违背了人们的普遍看法;Lorem Ipsum不是简单的随机文本

注:;可以是:,:-和(a)可以是(i)或任何类型的项目符号


Tags: 文本版本软件格式桌面段落行业世纪
1条回答
网友
1楼 · 发布于 2024-06-01 14:12:18

我假定你的要点总是用括号括起来,因为你没有另外说明。我正在利用这一点

下面是一个可能的解决方案:

import re
stri="Lorem Ipsum is simply dummy text of the printing and typesetting industry; (a) It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages; and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. (b) Contrary to popular belief; Lorem Ipsum is not simply random text."
matches=re.findall("\A(.*?)(?:(?:; )|(?:: )|(?:\-))(\(.*\).*?(?:\(|\Z)){0,}",stri)
bullets=re.findall(".*?\)(.*?)(?:\(|\Z)",matches[0][1])
liststr=[]
for i in bullets:
    liststr.append(matches[0][0]+i)

print(liststr)

由此产生的输出:

['Lorem Ipsum is simply dummy text of the printing and typesetting industry It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages; and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. ', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry Contrary to popular belief; Lorem Ipsum is not simply random text.']

相关问题 更多 >