python - 打印格式化列表(Python)
问题描述
如何打印列表并在每个元素后换行?例如,我想要这个列表: [1, 2, 3]
进行格式化,以便当我运行 print(list) 时,输出为:
[
'1'
'2'
'3'
]
我如何做到这一点?
解决方案
使用 json 模块,您可以使用缩进转储列表(任何 json 对象)。
import json
print(json.dumps(["1", "2", "3"], indent=1))
输出:
[
"1",
"2",
"3"
]
推荐阅读
- c - 为什么我的代码在输出中显示奇怪的数字?
- javascript - 带有实时 API 的 Angular 记住我的功能
- javascript - 迭代地图并在方法 Reactjs 中传递值
- android - 如何将命令的输出打印到吐司?
- ios - 在 Xcode 11.4 中找不到模块“KissXML”
- react-native - 如何在 react-native-elements 中的“PricingCard”中制作隐形按钮
- python-3.x - 如何从基于放置请求的无限滚动网站下载 HTML 内容...?
- python - 快速消除大列表中的“循环重复”(python)
- python-3.x - 即使我已连接,Pytube 连接错误
- ruby-on-rails - Heroku 构建失败找不到 webpack