python - 在 Python 中寻找 print 的替代方案

标签 python pprint

众所周知,pprint 在打印大量小单词列表时可能会“烦人”,因为 pprint 只能接受两种模式:一行多个小单词,或者每行多行小单词分别。

是否有其他一些 python 库可以以一种相当紧凑的方式打印像 {"1": [1] * 10, "2": [2]*100} 这样的字典?

谢谢!

最佳答案

传递 True 作为 compact 参数。 (仅适用于 Python 3.4+)

>>> pprint({"1" : [1]*10, "2": [2]*100}, compact=True)
{'1': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 '2': [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2]}

关于python - 在 Python 中寻找 print 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072724/

相关文章:

python - 过滤器线程安全吗

python - 在Shapely中查找多边形上最近点的坐标

python - 用额外的上下文装饰 as_view()

python - 为什么 lib pyttsx3 中的listen.listen 命令会打印这个额外的文本?

python - Python 中的 Namedtuple 格式化/ pretty-print

python - 如何让 python 的 pprint 返回一个字符串而不是打印?

python - Linux 中的沙盒

Python - 很好地打印大字典?

python - 如何使用 Python 的 pprint 模块漂亮地打印出嵌套在列表中的字典键值对?

python - 如何打印使用类实现的树数据结构?