Skip to content Skip to sidebar Skip to footer

Code Actualising Automatically Variables When Not Desired

I have isolated the following part of a bigger code: import numpy as np population= np.random.normal(0,1,5) individuals=population print(population) for i in range(len(individu

Solution 1:

use .copy() if you want to copy the content of the numpy array, what you are doing at the moment, is copying a pointer to the list. So both variables point to the same data, so if one changes they both change.

import numpy as np

population= np.random.normal(0,1,5)
individuals=population.copy()

print(population)

for i in range(len(individuals)):
    individuals[i]=0print(population)

For non-numpy lists you can use [:] eg

a = [1,2,3]
b = a[:]

Post a Comment for "Code Actualising Automatically Variables When Not Desired"