Programmatically get current Ipython notebook cell output?

前端 未结 3 1712
梦毁少年i
梦毁少年i 2020-12-08 15:11

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

3条回答
  •  一整个雨季
    2020-12-08 15:53

    You can get the output of the cell X by using _ or Out[X]. Such as:

    In [1]: 2 + 35
    Out[1]: 37
    In [2]: _ + 3
    Out[2]: 40 
    In [3]: lst = [i for i in range(5)]
            lst
    Out[3]: [0, 1, 2, 3, 4]
    In [4]: Out[1] #Using the output of Cell 1
    Out[4]: 37
    In [5]: Out[3][1] #Using the output of Cell 3
    Out[5]: 1
    

    Here, If you want to get the output of the previous cell, then you can use _. You can use two (__) or three underscores(___) as well to refer to output of the next previous and next-next previous cells respectively.

    However, if you have many cells in the notebook and you want to refer some particular cell, then Out[X] will be helpful.

提交回复
热议问题