Python代码第一次运行良好,但第二次运行失败

2024-09-25 08:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我第一次从笔记本上运行这段代码时,它运行得很好:

#Which letters and how many
letters = ["a","b","c"]
noOfLetters = len(letters)

#Looking for all permutations
resultA = []
from itertools import permutations
for i in range(noOfLetters):
    resultA.append(list(permutations(letters,i+1)))

如果我再次运行它(而不重新启动内核),则会出现以下错误:

^{pr2}$

Tags: and代码fromwhichforlen笔记本all
1条回答
网友
1楼 · 发布于 2024-09-25 08:34:03

假设“notebook”是Jupyter(以前是ipython notebooks),那么必须注意Jupyter保持所有变量的状态。在

>;这意味着第二次运行开始时,变量已初始化为第一次运行结束时的值。在

避免这种情况的一种方法是重新启动内核;另一种方法是删除所有变量;还有一种方法是每次运行时初始化所有变量。在

docs

To restart the kernel (i.e. the computational engine), click on the menu Kernel -> Restart. This can be useful to start over a computation from scratch (e.g. variables are deleted, open files are closed, etc...).

相关问题 更多 >