Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我将 Python 与numpy.
numpy
我有一个 numpy 数组b:
b
b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False])
我需要找到bequal的第一个和最后一个索引True。
True
对于此示例:
out_index: [0,2] [5,6] [8,11]
有人可以建议,我怎么得到out_index?
out_index
b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False]) idx = np.argwhere(np.diff(np.r_[False, b, False])).reshape(-1, 2) idx[:, 1] -= 1 print idx
输出:
[[ 0 2] [ 5 6] [ 8 11]]