python - Strange assignment in numpy arrays -
i have numpy array n rows of size 3. each row composed 3 integers, each 1 integer refers position inside numpy array. example if want rows refered n[4]
, use n[n[4]]
. visually:
n = np.array([[2, 3, 6], [12, 6, 9], [3, 10, 7], [8, 5, 6], [3, 1, 0] ... ]) n[4] = [3, 1 ,0] n[n[4]] = [[8, 5, 6] [12, 6, 9] [2, 3, 6]]
i building function modifies n, , need modify n[n[x]] specified x parameter (4 in example). want change 6 in subarray number (let's 0), use numpy.where find indexes, are
where_is_6 = np.where(n[n[4]] == 6)
now, if replace directly n[n[4]][where_is_6] = 0
there no change. if make previous reference var = n[n[4]]
, var[where_is_6]
change done locally function , n not changed globally. can in case? or doing wrong?
sounds need convert indices original n
's coordinates:
row_idxs = n[4] r,c = np.where(n[row_idxs] == 6) n[row_idxs[r],c] = 0
Comments
Post a Comment