python - 连接列表直到某个字符
问题描述
我有这个清单:
Jokes = ['First joke', 'Still first', '', 'Second Joke', 'Still second joke', 'Still 2nd joke']
我想将列表连接如下:
Jokes = ['First joke \n Still first', 'Second joke \n Still second joke \n Still 2nd joke']
这有可能吗?
谢谢,
解决方案
这是一个示例解决方案:
jokes = ['First joke', 'Still first', '', 'Second Joke', 'Still second joke', 'Still 2nd joke']
groups = [[]]
for part in jokes:
if part:
groups[-1].append(part)
else:
groups.append([])
result = [' \n '.join(joke) for joke in groups]
print(result)
推荐阅读
- javascript - ReactPlayer同时播放2个文件
- google-chrome - 如何根据员工姓名计算谷歌表格中的付款
- swift - 如何在 swift 中为 int enum 获得类似的 NSLog 打印结果?
- ios - NativeScript iOS 构建失败 Xcode - 错误:命令 Ld 失败,退出代码为非零
- amazon-web-services - 使用 SAM 向我的 lambda 函数添加 s3 事件触发器时遇到问题
- python - Linux下Python3如何将字符串拆分为中文字符列表
- python - 如何使用 re.findall 找到我想要的字符串?
- python-3.x - 将 Pandas DataFrame OrderedSet 列转换为列表
- json - 使用 jq 替换 json 中的争论
- next.js - Strapi - 如何允许作者角色发布他们的内容?