If I already appended something in lista_input
, whenever the parameter arr is modified inside the generabinario()
method (for instance: arr[I] = 0/1) also all the values in lista_input are modified in the exact same way.
What is going on?
That's my output for the given program:
[[1, 1], [1, 1], [1, 1], [1, 1]]
Of course the output is a sequence of [1, 1] because it's the last modification that the method does to the arr paramater.
And that's the code:
lista_input = []
def generabinario(n, arr, i):
if i == n:
lista_input.append(arr)
return
arr[i] = 0
generabinario(n, arr, i + 1)
arr[i] = 1
generabinario(n, arr, i + 1)
if __name__ == "__main__":
weights = []
results = []
print("Inserisci il numero di pesi da inserire")
num = int(input())
print("Inserisci i pesi")
for i in range(0, num):
weights.append(int(input()))
inputs = [None] * num
generabinario(2, inputs, 0)
print(lista_input)