python - 相当于 IPython 中的 Bash "!!"?

标签 python ipython

我正在寻找类似于 Bash 的功能'!!

在 Bash 中,如果输入 !! ,它将替换为您输入的最后一个命令。例如,如果您输入 which deactivate ,然后 cat $(!!) ,第二个命令将被重新格式化为 cat $(which deactivate) .

在 IPython 中输入 !!留下一个空列表,我怀疑它试图向我传递两个空字符串 shell 命令的输出列表。

是否有类似的方法将最后一个命令格式化为 IPython 中新输入的命令?

最佳答案

您可以通过引用输入编号来获取 IPython 中以前的任何输入:

In [4]: 'hello'
Out[4]: 'hello'

In [5]: In[4]
Out[5]: "'hello'"

如果你想抓取当前行之前的输入,可以使用 _iIn[-2] ( -1引用当前输入):

In [6]: In[-2]
Out[6]: 'In[4]'

In [7]: _i
Out[7]: 'In[-2]'

关于python - 相当于 IPython 中的 Bash "!!"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45958463/

相关文章:

python - 如何通过 HTTPie 使用空 json 数据发帖?

windows - 如何在 Windows 下的 IPython 中更改驱动器

python - 安装新模块后 IPython 内核重新启动

python - 在 Python 中创建 C 函数指针结构

python - Pandas,合并 2 个数据框

python - R 无法读取以 Feather 格式保存的 Python Pandas 数据帧

php - 正则表达式替换混合数字+字符串

python - 如何从 Heroku iPython 上的剪贴板粘贴?

ipython - 如何在 iPython session 中识别/查找自创建变量?

python - 在 python 中为大型 scipy.sparse 矩阵运算分配内存