2017-02-12 70 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