Array assigned to another does not hold the same value

Asked

Viewed 60 times

0

key=[[1,2,3,4],[5,6],[7,8,9,10]]
x=key
i=0
print(1,key)
while i<4:
    x[0][i]=x[2][i]
    i+=1
print(2,x)
print(3,key) #key era para se manter o mesmo valor, mas quando altera x, key está se alterando também, 

I simply want to assign the value of array key for x, but every time I change x, key also changes itself

In the terminal the print sai:


1 [[1, 2, 3, 4], [5, 6], [7, 8, 9, 10]]
2 [[7, 8, 9, 10], [5, 6], [7, 8, 9, 10]]
3 [[7, 8, 9, 10], [5, 6], [7, 8, 9, 10]]

4 answers

3

If it is a composite list, have nested lists, use deepcopy.

The difference between shallow and deep copy is relevant only to composite objects (objects containing other objects such as lists or class instances):

A superficial copy builds a new composite object and then (as far as possible) inserts references to objects found in the original.

A deep copy builds a new composite object and then recursively inserts copies of the found objects in the original.

import copy

key=[[1,2,3,4],[5,6],[7,8,9,10]]
x= copy.deepcopy(key)
i=0
print(1,key)
while i<4:
    x[0][i]=x[2][i]
    i+=1
print(2,x)
print(3,key) 

Upshot:

1 [[1, 2, 3, 4], [5, 6], [7, 8, 9, 10]]
2 [[7, 8, 9, 10], [5, 6], [7, 8, 9, 10]]
3 [[1, 2, 3, 4], [5, 6], [7, 8, 9, 10]]

Code in the Repl.it

0

When you assign directly x = key what you are going through not the object of key and yes the memory address, ie the id of the object. So when you make a change through x you change key because both are pointing to the same object in memory.

# Código de exemplo

nomes_registrados = ["joana","leandro","pedro","maria"]
nomes = nomes_registrados
nomes.pop(2)
print(nomes_registrados) # A saída será ['joana', 'leandro', 'maria']

To correct the code, use the method copy(). This method creates a copy of the current object but with a different address. See how the above example would look:

# Código de exemplo

nomes_registrados = ["joana","leandro","pedro","maria"]
nomes = nomes_registrados.copy()
nomes.pop(2)
print(nomes_registrados) # A saída será ['joana', 'leandro', 'pedro', 'maria']

The problem is that if you are going to use the method copy, should copy each object within your list, since your copy is passed to x would contain the memory addresses of objects equal to key within it.

So another way to solve your problem is by using the function copy.deepcopy module copy. Example:

# Código de exemplo

import copy
numbers = [[1,3,5],[2,4,6]]
list_2 = copy.deepcopy(numbers)
list_2.pop(1)
print(numbers) # Saída: [[1, 3, 5], [2, 4, 6]]
print(list_2) # Saída: [[1, 3, 5]]

-1

I’ll throw something in the air but technically in Python sharpeners do not exist, but I think by doing it that way yours x is "pointing" to the same object id as the key, ie you have not created a new variable simply has a new name pointing to the same object

Try to do this

key=[[1,2,3,4],[5,6],[7,8,9,10]]
x=key.copy()
i=0
print(1,key)
while i<4:
    x[0][i]=x[2][i]
    i+=1
print(2,x)
print(3,key)

Or even

 key=[[1,2,3,4],[5,6],[7,8,9,10]]
    x=list(key)
    i=0
    print(1,key)
    while i<4:
        x[0][i]=x[2][i]
        i+=1
    print(2,x)
    print(3,key)

Third and faster option

    key=[[1,2,3,4],[5,6],[7,8,9,10]]
    x=key[:]
    i=0
    print(1,key)
    while i<4:
        x[0][i]=x[2][i]
        i+=1
    print(2,x)
    print(3,key)

and with deepcopy for nestedLists

import copy

key=[[1,2,3,4],[5,6],[7,8,9,10]]
x=copy.deepcopy(key)
i=0
print(1,key)
while i<4:
    x[0][i]=x[2][i]
    i+=1
print(2,x)
print(3,key)

-1

When you do this assignment:

x=key

You do not have two lists, just copy the key reference to x. Then change in either of the two references will affect only one list, which is the same. To copy the contents of the string, you can use the copy method (available since Python 3.3):

x = key.copy()

Or still use the Slice

x = key[:]

Or use the constructor

x = list(key)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.