以编程方式获取当前的 IPython 笔记本单元输出? [英] Programmatically get current IPython notebook cell output?
问题描述
我有一个在 IPython 笔记本(输入单元 X)中运行的导入函数,它产生一个输出(在输出单元 X 中).函数运行后,我还有一些代码(也在输入单元格 X 中);该代码有没有办法检索当前输出(在输出单元格 X 中)?
I have an imported function that runs in an IPython notebook (input cell X) which produces an output (in output cell X). After the function runs, I have some more code (also in input cell X); is there any way for that code to retrieve the current output (in output cell X)?
可能还有其他方法可以完成我想要实现的目标;但我很好奇上述是否可行.
There may be other ways to do what I am trying to achieve; but I am curious if the above is possible.
推荐答案
IPython 的 输出缓存系统定义了几个全局变量:
IPython's output caching system defines several global variables:
- [
_
](单下划线):存储之前的输出,就像 Python 的默认解释器一样. - [
__
](两个下划线):下一个上一个. - [
___
](三个下划线):next-next previous.
- [
_
] (a single underscore): stores previous output, like Python’s default interpreter. - [
__
] (two underscores): next previous. - [
___
] (three underscores): next-next previous.
此外,在创建每个输出 x
之后,会创建一个变量 _
,以输出作为其值.例如:
Additionally, after each output x
is created, there is a variable _<x>
created with the output as its value. For example:
In [12]: lst = [i for i in range(11)]
In [13]: lst
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [14]: _13
Out[14]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
另外,如果你有兴趣,_i
包含输入单元格 x
的内容:
Also, if you're interested, _i<x>
contains the contents of the input cell x
:
In [15]: _i12
Out[15]: 'lst = [i for i in range(11)]'
这篇关于以编程方式获取当前的 IPython 笔记本单元输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!