任意尺寸的块状切片

编程入门 行业动态 更新时间:2024-10-24 20:17:11
本文介绍了任意尺寸的块状切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想切片一个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.

更多推荐

任意尺寸的块状切片

本文发布于:2023-11-08 15:48:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:块状   切片   尺寸

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!