1

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(individuals)):
    individuals[i]=0

print(population)

response:

[-0.1791731  -0.0756427   0.44463943 -0.51173395  0.9121922 ]
[0. 0. 0. 0. 0.]

I can't understand why the results are not identical.

Blackscholes
  • 45
  • 1
  • 8
  • I get an error: `name 'pop' is not defined`. Do you mean `print(population)`? – user3483203 May 14 '18 at 01:03
  • what is the variable `pop`? – johnashu May 14 '18 at 01:04
  • Also, the reason you get this output is because you point individuals as a reference to population, so you are changing population when you change individuals – user3483203 May 14 '18 at 01:04
  • Sorry changed the pop, that was a mistake – Blackscholes May 14 '18 at 01:05
  • you change each item to `0` in the for loop... what is it you are trying to achieve with the for loop? – johnashu May 14 '18 at 01:06
  • Actually the loop is bigger, but it's purpose is to changes the parameters among individuals, then I want to compare what changed compared to the initial ppopulation – Blackscholes May 14 '18 at 01:08
  • What is the desired outcome? What is the loop supposed to do. Can you provide the actual code you have problems with and where it is going wrong..We need a little more to go on.. – johnashu May 14 '18 at 01:10
  • Thank you very much chrisz, that was the post I needed (about copying lists) ! So many hours lost...but now I understand a bit better python – Blackscholes May 14 '18 at 01:12
  • Also I just created an account for this question and I am very impressed by your rapidity! Thank you again guys – Blackscholes May 14 '18 at 01:13
  • `individuals` is just another name for `population` so changing one changes the other. – martineau May 14 '18 at 01:13
  • @martineau I'm not sure if the liked duplicate is correct for this question, it mentions `[:]` but that won't work on this np list. you need to use the objects `.copy()`. However, the generic `copy.copy(old_list)` should work too, but that's not really the *proper* way to copy an np list. – ktzr May 14 '18 at 01:30
  • @ktzr: Perhaps the answer(s) aren't identical, but the problem this question asks about is, in my opinion...and it can be solved the same way—by making a copy of the list in whatever way is appropriate for its type. – martineau May 14 '18 at 01:31

1 Answers1

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]=0

print(population)

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

a = [1,2,3]
b = a[:]
ktzr
  • 1,625
  • 1
  • 11
  • 25
  • Oh I see, I just tested with [:] and as you told, it didn't work with numpy list. I am currently trying with .copy(), it works on the sample I gave here, but I am currently implementing it on my main code. I will come back to you if I have more problems. Thank you very much ! – Blackscholes May 14 '18 at 01:24
  • Use `population.copy()` not `[:]` as mentioned in the answer that is for non-numpy lists – ktzr May 14 '18 at 01:26