In below code, I expect to get :
[[1, 4, 6], [2, 3, 6], [2, 4, 5]]
But it return :
[[1, 3, 5], [1, 3, 5], [1, 3, 5]]
There are two problems :
- word in function b is a reference, not a new variable!!
- everything put in the child is a reference!!
My code :
def b(word,i):
word[i] = word[i]-1
return word
def a(individual):
child = []
for i in range(len(individual)):
child.append(b(individual,i))
return child
print(a([2,4,6,8]))