Python code works fine first time, but fails second time

2024/7/5 11:44:01

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 permutations8 for i in range(noOfLetters):
----> 9     resultA.append(list(permutations(letters,i+1)))TypeError: 'list' object is not callable
Answer

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

https://en.xdnf.cn/q/120622.html

Related Q&A

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

CSV IO python: converting a csv file into a list of lists

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?Im having trouble with this because some of my entries have a comma in thema file like: 1,2,3…

Sheet of paper in millimeters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Kivy App build with Buildozer. APK crash

I am using Oracle VirtualBox running Ubuntu 16. I have been able to build apk files for a while until my latest build. My program will run and keep its functionality when run with python 2.7 on the sam…

Python3 - convert csv to json using pandas

Ive got a .csv files with 5 columns but I only need the json file to contain 3 of these how would i go about doing it?csv file:Ncode Ocode name a b c 1 1.1 1x 1a 1b 1…

Python List of Dictionaries by Loops

I have 2 python list of dictionaries:[{index:1,color:red},{index:2,color:blue},{index:3,color:green} ]and[{device:1,name:x},{device:2,name:y},{device:3,name:z} ]How can I append each dictionary from th…

Removing parentheses and comma

Im importing Data from a database into python data frame. Now, I wish to use the data for further analysis, however, I need to do a little cleaning of the data before using. Currently, the required col…

Explicit Exception problem with try function

I have to run codes irrespective whether it fails or not. Im using ExplicitException. Following is my code:try:G.add_edge(data1[0][0],data1[1][0],weight=data1[2+i][0]) except ExplicitException:passtry:…

How do I perform a bubble sort in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ? aa= np.array([[348 ,345],[460 , 459 ]])value = 347prin…