返回多个范围的numpy数组 [英] Multiple ranges of numpy array returned

查看:110
本文介绍了返回多个范围的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设存在如下所示的数组:

Suppose there is an array like the below:

a = np.array([[1,2],
              [2,3],
              [2,3],
              [2,3],
              [4,5],
              [3,4],
              [2,3]])

如何返回多行,这就是我想要实现(我知道语法是错误的,但这会让您对我想要实现的想法有所了解):

How would I return multiple rows, this is what I would like to achieve (I know the syntax is wrong, but this will give you an idea of what I want to achieve):

a[0:2 & 5:6,:]

我无法传递单个行,因为在我的实际代码中需要包含更大的范围,例如 20:60 & 90:160 ,等等。

I would not be able to pass individual rows because in my actual code I would need to include larger ranges, e.g. 20:60 & 90:160, etc.

推荐答案

这有效:

>>> a[np.r_[:2, 5:6], :]
array([[1, 2],
       [2, 3],
       [3, 4]])

np.r _


将切片对象沿第一条轴转换为串联。

Translates slice objects to concatenation along the first axis.

这篇关于返回多个范围的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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