英文:
Given two numpy arrays, how to split one into an array of lists based on the second
问题
我有两个NumPy数组:一个包含任意值,另一个包含大于1的整数。这些整数的总和等于第一个数组的长度。示例:
values = np.array(["a", "b", "c", "d", "e", "f", "g", "h"])
lengths = np.array([1, 3, 2, 2])
len(values) == sum(lengths) # True
我想根据第二个数组的长度来分割第一个数组,并最终得到类似于以下的结果:
output = np.array([["a"], ["b", "c", "d"], ["e", "f"], ["g", "h"]], dtype=object)
使用Python循环遍历数组很容易,但当两个列表都非常大(包含数亿个元素)时,速度很慢。是否有一种方法可以使用原生的NumPy操作来执行此操作,这应该更快?
英文:
I have two numpy arrays: one containing arbitrary values, and one containing integers larger than 1. The sum of the integers is equal to the length of the first array. Sample:
values = np.array(["a", "b", "c", "d", "e", "f", "g", "h"])
lengths = np.array([1, 3, 2, 2])
len(values) == sum(lengths) # True
I would like to split the first array according to the lengths of the second array, and end up with something like:
output = np.array([["a"], ["b", "c", "d"], ["e", "f"], ["g", "h"]], dtype=object)
It's easy to iterate over the array with a Python loop, but it's also slow when both lists are very large (hundreds of millions of elements). Is there a way to do this operation using native numpy operations, which presumably should be must faster?
答案1
得分: 3
你可以使用numpy中的split
方法:
output = np.split(values, np.cumsum(lengths))[:-1]
英文:
You can use the split
method from numpy:
output = np.split(values, np.cumsum(lengths))[:-1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论