使用条件从数组获取索引值

时间:2019-11-08 05:26:35

标签: python-3.x

我有一个类似的np数组。

a = [ [0. 0. 1. 0.]
  [0. 1. 0. 0.]
  [1. 0. 0. 0.]
  [0. 0. 1. 0.]
  [0. 0. 1. 0.]
]

如果项目值为== 1,我想获取第三列的所有行索引

a[:,2:2+1]==1

在这种情况下,我的结果是 索引= [0 3,3] 我可以使用任何功能吗?

1 个答案:

答案 0 :(得分:0)

import numpy as np
a=np.array([[0, 0, 1, 0],[0, 1, 0, 0],[1,0, 0, 0],[0, 0, 1, 0]])
index,value_first_at_index=np.where(a[:,2:3]==1)
print(index)

e