Python代码第一次可以正常运行,但是第二次失败

编程入门 行业动态 更新时间:2024-10-12 01:23:55
本文介绍了Python代码第一次可以正常运行,但是第二次失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我第一次从Notebook运行此代码块时,效果很好:

The first time I run this block of code from Notebook it works fine:

#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)))

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

If I run it again (without restarting the Kernel) I get the following error:

TypeError Traceback (most recent call last) <ipython-input-5-4050a4ce7a36> in <module>() 7 from itertools import permutations 8 for i in range(noOfLetters): ----> 9 resultA.append(list(permutations(letters,i+1))) TypeError: 'list' object is not callable

推荐答案

假设笔记本"是Jupyter(以前是ipython笔记本),则必须注意jupyter保留所有变量的状态.

Assuming "notebook" is Jupyter (previously ipython notebooks), you must be careful that jupyter keeps the state of all variables.

->表示第二次运行从已经初始化为第一次运行结束时具有的值的变量开始.

--> that means that the second run starts with variables already initialized at the value they had at the end of the first run.

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

One way to avoid that is to restart the kernel; another is to delete all variables; one more is to initialize all your variables each time you run.

来自文档:

要重新启动内核(即计算引擎),请单击菜单内核->重新启动.这对于从头开始计算非常有用(例如,删除变量,关闭打开的文件等).

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...).

更多推荐

Python代码第一次可以正常运行,但是第二次失败

本文发布于:2023-11-23 04:27:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正常运行   代码   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!