【问题标题】:Why does Jupyter notebook NOT execute all the lines of code? (iPython)为什么 Jupyter notebook 不执行所有代码行? (iPython)
【发布时间】:2020-07-02 06:27:00
【问题描述】:
ls = [16, 81, 23, 74, 91, 612, 33, 812]

ls[2:6:3] #Starting from index 2 till index 5 and skip the next 2 indices
ls[::-1]  # Reverse list is printed

只执行第二行,即ls[::-1]。我做错了什么?

【问题讨论】:

  • Jupyter 只会输出块中最后一个表达式的结果。如您所见,所有语句都已执行。第二条语句被执行,但结果被丢弃,因为它没有分配给任何东西。
  • @JanChristophTerasa 谢谢,但有什么办法可以同时显示两者吗?
  • print 它们,或者分成两个块,或者将两个表达式放在一行上(这将返回一个元组)。

标签: python python-3.x jupyter-notebook ipython


【解决方案1】:

它确实执行了所有行,默认情况下它只输出最后一行。 如果你想看到所有的步骤,你可以print你想看到的结果。

This answer 还提供了一个替代方法,要求 jupyter 通过添加系统地显示所有输出

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

【讨论】:

    【解决方案2】:

    请重新启动您的 jupyter-notebook 并同时刷新 jupyter。希望对您有所帮助..

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多