给定两个NumPy数组,如何根据第二个数组将一个数组分割为一个列表数组

huangapple go评论59阅读模式
英文:

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]

huangapple
  • 本文由 发表于 2023年2月8日 23:30:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定