Getting length of “slices” of NumPy array (or list) without actually slicing

只谈情不闲聊 提交于 2021-01-29 04:16:41

问题


Suppose that I have a NumPy array arr like

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

and a few included indices in another array, inds,

array([3, 6])

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].


I know that I could use np.split to do

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

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?


回答1:


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])))

Shorter alternative -

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

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]


来源:https://stackoverflow.com/questions/42370491/getting-length-of-slices-of-numpy-array-or-list-without-actually-slicing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!