Home > Mobile >  Why is this array changing when I'm not operating on it?
Why is this array changing when I'm not operating on it?

Time:03-04

I have two arrays:

order = np.array([ 0, 1,  2,  3,  4,  5,  6, 10,  7,  8,  9])
X = np.array([[1,1], [1,2], [2,1], [1,7], [7,3], [8,3], [8,2], [10,5], [10,6], [10,7], [10,1]]

And I'm running the foll)owing code:

m,n = X.shape
for i in range(m):
     print( i," ",X[order[i]])

I get the following result:

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

Why are the last elements changed? I don't see why X is changed by indexing some elements.

edit: added np.array

CodePudding user response:

import numpy
# your data (order and X)
m, n = numpy.shape(X)
for i in range(m):
    print(i, " ", X[order[i]])

Output:

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