Jupyter Notebook(或vscode插件) 一个cell有多个输出

发布时间 2023-03-26 10:50:35作者: 树娃娃

方法一

在文件的开头加上如下代码,该方法仅对当前文件有效

from IPython.core.interativeshell import InteractiveShell
InteractiveShell.ast_node_interctivity = "all"

方法二

添加配置文件,该方法对所有文件生效
1.在终端输入

vi ~/.ipython/profile_default/ipython_config.py

2.shift + G 跳到最后一行, 按i开始编辑

c = get_config()
c.InteractiveShell.ast_node_interactivity = "all"

esc 跳到命令模式,然后按 : 冒号键,最后再按wq保存退出

3.重启

方法三

使用元组(tuple)或字典(dictionary)等数据结构,将多个值打包成一个单一的对象将其返回

例:使用元组

def multiple_results():
  result1 = 1
  result2 = 2
  result3 = 3
  return result1, result2, result3

result = multiple_results()
print(result) #(1, 2, 3)