简体   繁体   English

使用索引数组(Python)将numpy数组划分为多个数组

[英]Divide numpy array into multiple arrays using indices array (Python)

I have an array: 我有一个数组:

a = [1, 3, 5, 7, 29 ... 5030, 6000]

This array gets created from a previous process, and the length of the array could be different (it is depending on user input). 此数组是从先前的进程创建的,并且数组的长度可能不同(它取决于用户输入)。

I also have an array: 我也有一个数组:

b = [3, 15, 67, 78, 138]

(Which could also be completely different) (也可能完全不同)

I want to use the array b to slice the array a into multiple arrays. 我想使用阵列b切片阵列a成多个阵列。

More specifically, I want the result arrays to be: 更具体地说,我希望结果数组为:

array1 = a[:3]
array2 = a[3:15]
...
arrayn = a[138:]

Where n = len(b) . 其中n = len(b)

My first thought was to create a 2D array slices with dimension (len(b), something) . 我的第一个想法是创建一个带维度的二维数组slices (len(b), something) However we don't know this something beforehand so I assigned it the value len(a) as that is the maximum amount of numbers that it could contain. 但是,我们不知道这个something提前,所以我赋予它的价值len(a)因为这是它可能包含数字的最高金额。

I have this code: 我有这个代码:

 slices = np.zeros((len(b), len(a)))

 for i in range(1, len(b)):
     slices[i] = a[b[i-1]:b[i]]

But I get this error: 但我得到这个错误:

ValueError: could not broadcast input array from shape (518) into shape (2253412)

You can use numpy.split : 你可以使用numpy.split

np.split(a, b)

Example : 示例

np.split(np.arange(10), [3,5])
# [array([0, 1, 2]), array([3, 4]), array([5, 6, 7, 8, 9])]
b.insert(0,0)
result = []
for i in range(1,len(b)):
    sub_list = a[b[i-1]:b[i]]
    result.append(sub_list)
result.append(a[b[-1]:])

You are getting the error because you are attempting to create a ragged array. 您收到错误是因为您正在尝试创建一个不规则的数组。 This is not allowed in numpy. numpy中不允许这样做。

An improvement on @Bohdan's answer: 对@ Bohdan的回答有所改进:

from itertools import zip_longest
result = [a[start:end] for start, end in zip_longest(np.r_[0, b], b)]

The trick here is that zip_longest makes the final slice go from b[-1] to None , which is equivalent to a[b[-1]:] , removing the need for special processing of the last element. 这里的技巧是zip_longest使最终切片从b[-1]变为None ,这相当于a[b[-1]:] ,从而无需对最后一个元素进行特殊处理。

Please do not select this. 请不要选择此项。 This is just a thing I added for fun. 这只是我为了好玩添加的东西。 The "correct" answer is @Psidom's answer. “正确”的回答是@ Psidom的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM