有效地发现与非零点在SciPy的/ numpy的Python中的时间间隔? [英] efficiently finding the interval with non-zeros in scipy/numpy in Python?

查看:347
本文介绍了有效地发现与非零点在SciPy的/ numpy的Python中的时间间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Python列表或一个python 1-D阵列(再在numpy的psented $ P $)。假定有元素如何可以找到在该列表或阵列的非零的伸展开始和结束坐标(即,索引)的连续段?例如,

suppose I have a python list or a python 1-d array (represented in numpy). assume that there is a contiguous stretch of elements how can I find the start and end coordinates (i.e. indices) of the stretch of non-zeros in this list or array? for example,

a = [0, 0, 0, 0, 1, 2, 3, 4]

nonzero_coords(一)应返回[4,7]。为:

nonzero_coords(a) should return [4, 7]. for:

b = [1, 2, 3, 4, 0, 0]

nonzero_coords(二)应返回[0,2]。

nonzero_coords(b) should return [0, 2].

感谢。

推荐答案

假设有非零元素的单个连续伸展...

Assuming there's a single continuous stretch of nonzero elements...

x = nonzero(a)[0]
result = [x[0], x[-1]]

这篇关于有效地发现与非零点在SciPy的/ numpy的Python中的时间间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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