[:] Cut only a shallow copy of the list?

I got special errors from this copy [:]

.

The docs say [:]

only makes a shallow copy, but it seems:

a = [1,2,3]
id(a)
3071203276L
b=a[:]
id(b)
3071234156L

      

id(a)

not equal id(b)

; how is it just a shallow copy?

Specific case:

import numpy as np
import random
a = np.array([1,2,3])
b=a[:]
random.shuffle(a)

      

b

changes accordingly.

+3


source to share


5 answers


This is a shallow copy, but the change has b

no effect a

in this case, because the items are just numbers. If they were links, it a

will update:

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

b[1] = 5
print "a: ", a
print "b: ", b
# a: [1, 2, 3]
# b: [1, 5, 3]

      



against

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

b[1][0] = 5
print "a: ", a
print "b: ", b
# a:  [[1], [5], [3]]
# b:  [[1], [5], [3]]

      

+3


source


Answer to figure:

Arrays in numpy represent views / indexes in a backup store.

You can copy the view without copying the backup ...



a=numpy.array([1,2,3,4])
b=a[:] # copy of the array ("view" or "index"), not the storage
b.shape=(2,2)
print a
# [1 2 3 4]
print b
# [[1 2]
#  [3 4]]
b *= 2
print a
# [2 4 6 8]
print b
# [[2 4]
#  [6 8]]

      

See how change b changes? But they still have a different form. Consider them as a kind of data; and the line b=a[:]

only copies that view. I might even change the shape b

. Because it is just an index on the data that says where the columns and rows are in memory.

If you want to get a copy of the backup store in numpy use a.copy()

.

+5


source


The difference is that the elements of list a and list b are the same. Changing modified objects in any list also affects the items in the other list.

In contrast, there is a deep copy that tries to create a completely different set of objects.

0


source


Probably the only thing doing a deep copy is deepcopy

.

Because deep copies are expensive, because you need to keep track of all the objects you copy.

Consider these bad structures:

a = []
a.append(a)

      

Of course you don't want to make a naive deep copy of this.

So this is a shallow copy. But in your example, this is storing primitives to be copied as a value and not as a reference. Therefore, changing one list will not change the other.

id()

cannot be used to highlight shallow and deep copies

It distinguishes copies from non-pixels (which have the same ID).

a = [1, 2, 3]
b = a
print id(b), id(a), "no surprise, same id, no copy."

      

Arrays of different types

Here you are only "shallow copy" the index, not the data stored in the backup. Use .copy()

if you want to make sure you have a copy.

0


source


Numpy determines which chunk is returned differently in the python standard library. This is because numpy is used to work with a huge amount of data. Copying these huge arrays is not always necessary, especially if the user only needs a temporary representation of the array. For example, it arr[:100].sum()

sums the first 100 elements without creating a temporary shallow copy of the first 100 elements. Note that temporary views are created for the main slice only.

For details see.

0


source







All Articles