0

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]

numpy array

>>> c = numpy.array([1,2,3,4])
>>> d = c[1:3]
>>> d[1] = 0
>>> c
array([1, 2, 0, 4])
>>> d
array([2, 0])

as it is seen in numpy array c is effected directly. I think in built-in lists, new memory is allocated for the variable b. Probably in numpy the reference of c[1:3] is assigned d, I am not clear about these. How this works for numpy and built-in?

asked Nov 5, 2016 at 14:58
4
  • Possible duplicate of array vs vector vs list Commented Nov 5, 2016 at 15:01
  • @polka thanks for quick answer but it is not enough clear for me. Commented Nov 5, 2016 at 15:13
  • Note that it is not correct to talk of a numpy list. It is a numpy array. Commented Nov 5, 2016 at 16:04
  • @polka, your link is about c++, this question is about specific Python objects. Very different issues. Commented Nov 5, 2016 at 16:08

2 Answers 2

3

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.

answered Nov 5, 2016 at 15:58
3

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)

answered Nov 5, 2016 at 20:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.