我不明白python中pprint中的宽度字段 [英] I don't understand the width field in pprint in python
问题描述
我不是很清楚这个概念.有人能给我一些例子来演示python中pprint中宽度的概念吗?
I don't understand this concept clearly. Could somebody give me some examples to demonstrate the concept for width in pprint in python?
推荐答案
基本上它会尝试将您的输出限制为特定的宽度.
Basically it tries to limit your output to a specific width.
这是一个例子:
import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
pp = pprint.PrettyPrinter(width=80)
pp.pprint(stuff)
结果是:
['spam', 'eggs', 'lumberjack', 'knights', 'ni']
但是如果你做同样的事情但是改变宽度(比如 10):
but if you do the same thing but change the width(say to 10):
import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
pp = pprint.PrettyPrinter(width=10)
pp.pprint(stuff)
你得到:
['spam',
'eggs',
'lumberjack',
'knights',
'ni']
这是从 python 文档 ( http://docs.python.org/图书馆/pprint.html).对于这样的事情,我发现打开 python 中断器并播放并查看命令如何对您输入的内容做出反应更容易.
This is a modified example from the python docs ( http://docs.python.org/library/pprint.html ). For things like this, I find it easier to just open the python interrupter and play around and see how the commands react to what you enter.
这篇关于我不明白python中pprint中的宽度字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!