Skip to content Skip to sidebar Skip to footer

Numpy Vs Built-in Copy List

what is the difference below codes built-in list code >>> a = [1,2,3,4] >>> b = a[1:3] >>> b[1] = 0 >>> a [1, 2, 3, 4] >>> b [2, 0]

Solution 1:

The key point to understand is that every assignment in Python associates a name with an object in memory. Python never copies on assignment. It now becomes important to understand when new objects are created and how they behave.

In your first example, the slicing in the list creates a new list object. In this case, both of the lists reference some of the same objects (the int 2 and the int 3). The fact that these references are copied is what is called a "shallow" copy. In other words, the references are copied, but the objects they refer to are still the same. Keep in mind that this will be true regardless of the type of thing that is stored in the list.

Now, we create a new object (the int 0) and assign b[1] = 0. Because a and b are separate lists, it should not surprise us that they now show different elements.

I like the pythontutor visualisation of this situation.

In the array case, "All arrays generated by basic slicing are always views of the original array.".

This new object shares data with the original, and indexed assignment is handled in such a way that any updates to the view will update the shared data.

Solution 2:

This has been covered alot, but finding a good duplicate is too much work. :(

Let's see if I can quickly describe things with your examples:

>>>a = [1,2,3,4]    # a list contains pointers to numbers elsewhere>>>b = a[1:3]   # a new list, with copies of those pointers>>>b[1] = 0# change one pointer in b>>>a
[1, 2, 3, 4]    # does not change any pointers in a
>>>b
[2, 0]

An array has a different structure - it has a data buffer with 'raw' numbers (or other byte values).

numpy array
>>>c = numpy.array([1,2,3,4])>>>d = c[1:3]     # a view; a new array but uses same data buffer>>>d[1] = 0# change a value in d; >>>c
array([1, 2, 0, 4])   # we see the change in the corrsponding slot of c
>>>d
array([2, 0])

The key point with lists is that they contain pointers to objects. You can copy the pointers without copying the objects; and you can change pointers without changing other copies of the pointers.

To save memory and speed numpy as implemented a concept of view. It can make a new array without copying values from the original - because it can share the data buffer. But it is also possible to make a copy, e.g.

 e =c[1:3].copy()
 e[0]=10# no change in c

view v copy is a big topic in numpy and a fundamental one, especially when dealing with different kinds of indexing (slices, basic, advanced). We can help with questions, but you also should read the numpy docs. There's no substitute for understanding the basics of how a numpy array is stored.

http://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html

http://www.scipy-lectures.org/advanced/advanced_numpy/ (may be more advanced that what you need now)

Post a Comment for "Numpy Vs Built-in Copy List"