简体   繁体   中英

Finding a row in 2d Numpy array with values x,y,z in

I have

import numpy as np
np_array = np.array([[0,4],[0,5],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,0]==6)and np.where(np_array[:,1]==1)
print(np_array[rows])

Expected answer

[6,1]

Answer from code

[[9 1]
 [6 1]]

What I would like is the index of where [6,1] is.

I must be missing something.

Without where , just using indexing.

import numpy as np

np_array = np.array([[0, 4], [0, 5], [3, 5], [6, 8], [9, 1], [6, 1]])

rows = (np_array[:, 0] == 6) & (np_array[:, 1] == 1)

print(np_array[rows][0])

You can get the indices with np.where(rows) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM