获得“切片"的长度;没有实际切片的NumPy数组(或列表) [英] Getting length of "slices" of NumPy array (or list) without actually slicing

查看:37
本文介绍了获得“切片"的长度;没有实际切片的NumPy数组(或列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像

array([5, 3, 0, 5, 7, 6, 5, 9, 0, 6])

以及一些包含在另一个数组中的索引 inds

and a few included indices in another array, inds,

array([3, 6])

我要生成一个数组,该数组的长度为 arr 的子数组,我用 inds .因此,在这种情况下,我的结果将是 [3、3、4] .

I want to generate an array with the lengths of the subarrays of arr were I to split my array with inds. So in this case, my result would be [3, 3, 4].

我知道我可以使用 np.split 进行

>>> np.split(arr, inds)
[array([5, 3, 0]), array([5, 7, 6]), array([5, 9, 0, 6])]

并映射 size()从那里获得 [3,3,4] 的正确结果,但是实际上似乎不必要的花费假定我只是使用子数组的大小来拆分数组-显然,该信息无需拆分即可(从索引中获取),但是我如何有效地使用它呢?

and map size() to get the correct result of [3, 3, 4] from there, but it seems like an unnecessary cost to actually split the array given that I'm just using the size of the subarrays - this information is obviously available without splitting (from the indices), but how can I effectively use it?

推荐答案

一种方法是在索引数组的两侧串联端点(0和数组的长度),然后使用差分来获取间隔长度-

One approach with concatenation of the endpoints (0 and the length of array) on either sides of the indices array and then use diferentiation to get the interval lengths -

np.diff(np.concatenate(([0], inds, [arr.size])))

更短的选择-

np.diff(np.r_[0, inds, arr.size])

为了提高性能,我们可以使用一次移开的切片之间的差异将差异替换为 np.diff -

For performance we could use difference between one-off shifted slices to replace the differentiation with np.diff -

inds_ext = np.concatenate(([0], inds, [arr.size]))
out = inds_ext[1:] - inds_ext[:-1]

这篇关于获得“切片"的长度;没有实际切片的NumPy数组(或列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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