0

I have issues understanding how Arrays work in python.

I wrote this short demo code:

from numpy import zeros

a = zeros((3), 'd')
b = zeros((2,3), 'd')
for i in range(2):
    for j in range(3):
        a[j] = i*j
    b[i] = a
print "A: " + str(a) + "\n"
print "B: " + str(b)

The Output of this is:

A: [ 0.  1.  2.]
B: [[ 0.  0.  0.] [ 0.  1.  2.]]

So here is my question. Why isn't the output for this:

A: [ 0.  1.  2.]
B: [[ 0.  1.  2.] [ 0.  1.  2.]]

Because I made the changes in the same a and the address of the array hasn't changed.

6
  • because you changed the reference of all b sublists with a Commented Feb 16, 2017 at 12:54
  • for i in range(2) -> your first loop features value i=0, setting a[j] = 0 for all j -> b[0] = [0. 0. 0.] Commented Feb 16, 2017 at 12:58
  • Yes but it is the same adress. All sublist of b Point to the same adress. So in the end the values should be the last i write in the array or not ? Commented Feb 16, 2017 at 12:58
  • b[0] is not a sublist - it is a row in the array. Commented Feb 16, 2017 at 13:05
  • I think the question is, why isn't b[0,:] a view of a? Why a copy? Is b[1,:] a view or copy? What happens with lists' Commented Feb 16, 2017 at 13:06

3 Answers 3

1

You appear to be assuming that

b[i] = a

inserts a reference to a into b. That is not the case. Assignment to an array slice copies the data. This behaviour is similar to slice assignment with lists.

Maybe the confusion comes from it being different the other way round?

a = b[i]

does not copy, it creates a view; this is different to list slicing.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah. Okay i thought it was like in Java or C++ where it is only a reference. Thank you!
0

Its [ 0. 0. 0.] because:

the first loops start with "0" so you have for "i" the zero:

a[j] = 0*j

everything multiplied with 0 is zero.

1 Comment

Yeah that is clear. My Problem is the following. In the second Iteration i write the values [0,1,2] in the array a. Now lets say the adress of a is 0x2A. So b[0] points to the adress 0x2A and b[1] does also point to the adress 0x2A. And 0x2A is the array a and the last entries i made for a was [0,1,2]. So the Content of b should be [[0,1,2],[0,1,2]]
0

Arrays do not exhibit the behaviour you are expecting, however Lists will. See the following example:

import numpy as np

a = np.zeros(3, 'd')
b = np.zeros(3, 'd')
c = np.zeros((2,3), 'd')

c[0] = a
c[1] = b

a[1] = 2
b[0] = 1

print 'Matrix A: {}'.format(a)
print 'Matrix B: {}'.format(b)
print 'Matrix C: {}'.format(c)

a = [0, 0, 0]
b = [0, 0, 0]
c = [a, b]

a[1] = 2
b[0] = 1

print 'List A: {}'.format(a)
print 'List B: {}'.format(b)
print 'List C: {}'.format(c)

When we use matrices, c[0] copies the current value of a, c[1] does the same for b. The references are lost, so modifications to a and b have no effect on c. Hence, we print:

Matrix A: [ 0.  2.  0.]
Matrix B: [ 1.  0.  0.]
Matrix C: [[ 0.  0.  0.]
 [ 0.  0.  0.]]

However for lists, the references are retained. Hence, we print:

List A: [0, 2, 0]
List B: [1, 0, 0]
List C: [[0, 2, 0], [1, 0, 0]]

Comments

Your Answer

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.