numpy的:在一个数组搜索第一个和最后一个索引的 [英] numpy: search of the first and last index in an array

查看:301
本文介绍了numpy的:在一个数组搜索第一个和最后一个索引的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python与 numpy的

我有一个numpy的阵列 B

  B = np.array([真,真,真,假,假,真,真,假,真,真,真,真,假])

我需要找到第一个和最后一个索引,其中 B 等于

有关此exsample:

  out_index:[0,2]
           [5,6]
           [8,11]

有人可以请建议,?我如何获得 out_index


解决方案

  B = np.array([真,真,真,假,假,真,真,假,真,不错,不错,真,假])
IDX = np.argwhere(np.diff(np.r_ [假,B,FALSE))。重塑(-1,2)
IDX [:, 1] - = 1
打印IDX

输出:

  [2:0]
 [5 6]
 [8月11日]

I use Python with numpy.

I have a numpy array b:

b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False])

I need to find the first and last index where b is equal True.

For this exsample:

out_index: [0,2]
           [5,6]
           [8,11]

Can someone please suggest, how do I get 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

output:

[[ 0  2]
 [ 5  6]
 [ 8 11]]

这篇关于numpy的:在一个数组搜索第一个和最后一个索引的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆