如何使Ipython输出一个列表后没有换行符?

cls*_*udt 23 python list ipython ipython-notebook

IPython控制台打印带有换行符的元素列表,以便每个元素都显示在自己的行中.这通常是一个功能,但在我的情况下它是一个错误:我需要复制和粘贴长列表,所以我需要一个紧凑的表示.我怎样才能做到这一点?

fal*_*tru 40

您可以使用%pprint命令打开/关闭pprint功能:

In [1]: range(24)
Out[1]:
[0,
 1,
 2,
 ...
 21,
 22,
 23]

In [2]: %pprint
Pretty printing has been turned OFF

In [3]: range(24)
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
Run Code Online (Sandbox Code Playgroud)

如果要pprint永久关闭,请创建配置文件,然后添加c.PlainTextFormatter.pprint = False到配置文件.

Linux示例:

$ ipython profile create
[ProfileCreate] Generating default config file: '.../ipython_config.py'
[ProfileCreate] Generating default config file: u'..../ipython_notebook_config.py'
$ echo 'c.PlainTextFormatter.pprint = False' >> ~/.ipython/profile_default/ipython_config.py
Run Code Online (Sandbox Code Playgroud)


Jos*_*ode 5

完全关闭漂亮打印的另一种方法是max_width增加PlainTextFormatter.

添加以下内容ipython_config.py(通过 查找ipython locate profile):

c.PlainTextFormatter.max_width = 120
Run Code Online (Sandbox Code Playgroud)

这将允许漂亮的打印机使用更少的垂直空间,允许行扩展到 120 个字符而不是默认的 79 个字符。