python - 如何将列表的行合并成短语(帮助完成代码)?
问题描述
我有一个清单如下:
[
'"The investment would also benefit the company’s',
'ongoing commitment to the Kraft Foods Sustainability Programme; substantially reducing material',
'usage by replacing cardboard boxes with reusable',
'plastic tote bins and cutting daily vehicle movements through the accurate management of',
'fixed cyling schedules.",',
'"The core brands of Cadbury Roses and Heroes are',
'produced and packaged at the Bournville production facility. The wrapped chocolates that make',
'up these assortments have traditionally been',
'managed through the factory in cardboard boxes.'
]
我需要"
将行删除并合并到短语中才能获得以下结果:
[
'The investment would also benefit the company’s ongoing commitment to the Kraft Foods Sustainability Programme; substantially reducing material usage by replacing cardboard boxes with reusable plastic tote bins and cutting daily vehicle movements through the accurate management of fixed cyling schedules.',
'The core brands of Cadbury Roses and Heroes are produced and packaged at the Bournville production facility.",
'The wrapped chocolates that make up these assortments have traditionally been managed through the factory in cardboard boxes.'
]
我该怎么做?
这就是我开始做的事情:
final_list = []
temp_list = []
for l in lines:
temp_list.append(l)
if "." in l:
phrase = ' '.join(map(str, temp_list))
final_list.append(phrase)
temp_list = []
final_list
它生成以下输出,这与我的预期输出不完全匹配:
[
'"The investment would also benefit the company’s ongoing commitment to the Kraft Foods Sustainability Programme; substantially reducing material usage by replacing cardboard boxes with reusable plastic tote bins and cutting daily vehicle movements through the accurate management of fixed cyling schedules.",',
'"The core brands of Cadbury Roses and Heroes are produced and packaged at the Bournville production facility. The wrapped chocolates that make',
'up these assortments have traditionally been managed through the factory in cardboard boxes.'
]
解决方案
试试下面的。它只会检查结尾是否有句点,它会删除您在测试和输出案例之间删除的额外引号和逗号,但不会删除额外字符。
final_list = []
temp_list = []
for l in lines:
temp_list.append(l)
if "." in l[-3:]:
phrase = ' '.join(map(str, temp_list)).strip('", ')
final_list.append(phrase)
temp_list = []
print(final_list)
推荐阅读
- python - Matplotlib 单击时删除行
- redux-toolkit - Redux 工具包切片。场缩减器中的纯函数
- javascript - 如何根据ID获取文档下的所有数据
- php - 无法使用 localhost 和 amps 登录
- android - 在 mockk 中模拟嵌套属性
- c - 为什么 (-7 << 16) | 75有一些额外的位?
- flutter - 为什么解析映射数据到对象会颤动
- python - 更新单行的不同方式之间的区别
- react-native - 给定 oauth_app 资源未找到 - Magic.Link
- azure - WebChat - 直线语音适配器错误:WebSocket 连接失败,出现 400