以编程方式获取当前的Ipython笔记本电脑输出? [英] Programmatically get current Ipython notebook cell output?

查看:62
本文介绍了以编程方式获取当前的Ipython笔记本电脑输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在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.

谢谢!

推荐答案

< IP> 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 ,创建一个变量 _< 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> 包含输入单元格的内容 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆