任意尺寸的块状切片 [英] Numpy slice of arbitrary dimensions

查看:62
本文介绍了任意尺寸的块状切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想切片一个numpy数组以获得最后一个维度中的第i个索引.对于3D阵列,应为:

I would like to slice a numpy array to obtain the i-th index in the last dimension. For a 3D array, this would be:

slice = myarray[:,:,i]

但是我正在编写一个函数,可以获取任意维度的数组,因此对于4D数组,我需要myarray[:,:,:,i],依此类推.有没有一种方法可以为任何数组获取此切片,而无需显式编写数组维?

But I am writing a function where I can take an array of arbitrary dimensions, so for a 4D array I'd need myarray[:,:,:,i], and so on. Is there a way I can obtain this slice for any array without explicitly having to write the array dimensions?

推荐答案

...Ellipsis,它们正是这样做的:

There is ... or Ellipsis, which does exactly this:

slice = myarray[..., i]

省略号是python对象,如果您想在方括号表示法之外使用它.

Ellipsis is the python object, if you should want to use it outside the square bracket notation.

这篇关于任意尺寸的块状切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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