Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

jupyter notebook - Python code works fine first time, but fails second time

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...