2017-02-12 80 views
2

问题

当这些宽度比当前屏幕宽时,IPython决定垂直显示列表(并强制您滚动很多)。见下面IPython中的控制列表可视化

In [1]: list(range(20)) 
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 

In [2]: list(range(30)) 
Out[2]: 
[0, 
1, 
2, 
... 
27, 
28, 
29] 

尽管Python总是水平显示它们。 有没有办法在IPython中控制这种行为?我在网上找不到任何东西。也许我一直在使用错误的关键字。

重复后验

好的。现在我得到了一个答案,我也发现(使用pprint关键字),这个问题是How to make Ipython output a list without line breaks after elements?的重复。

回答

2

您可以使用命令%pprint来控制它。

正如你写,

list(range(30)) 

产生

[0, 
1, 
2, 
3, 
4, 
... 
27, 
28, 
29] 

如果你再输入

%pprint 

你会读到的消息

Pretty printing has been turned OFF 

然后

list(range(30)) 

变为:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] 

或者,您也可以只输入

print(list(range(30)) 

它给你也行输出。

+1

谢谢。行为原因是'%pprint'。我使用的是Python 3,所以我仍然需要从'range'对象中构建一个'list'。 – Atcold

0

你可以尝试使用pprint的pformat函数。

0

你绝对可以使用print

但是对于你的问题,它与IPython交互式终端的最大线宽设置有关。

只需在ipython默认配置目录(通常位于~/.ipython/profile_default/)中编辑或创建IPython配置文件ipython_config.py。添加以下行,然后在终端中重新启动IPython。

c = get_config() 
# the default is 79, modify it to any max line width you like 
# list repr string larger than it will be wrapped to display vertically 
c.PlainTextFormatter.max_width = 100 

参见Introduction to IPython configurationipython: how to set terminal width