使用Python中的其他两个数组设置的数组元素/数量创建新的数组

时间:2022-12-25 12:53:29

I have two arrays in Python (numpy arrays):

我在Python中有两个数组(numpy数组):

a=array([5,7,3,5])
b=array([1,2,3,4])

and I wish to create a third array with each element from b appearing a times in the new array, as:

我希望创建第三个数组,其中b中的每个元素在新数组中出现a次,如下所示:

c=array([1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,4,4,4,4,4])

Is there a fast, numPythonic way of doing this with a minimum of looping? I need to use this operation thousands of times in a loop over a fairly large array, so I would like to have it be as fast as possible.

有没有一种快速的,用最少的循环来实现的方法?我需要在一个相当大的数组的循环中使用这个操作数千次,所以我希望它尽可能快。

Cheers, Mike

干杯,迈克

1 个解决方案

#1


5  

I believe repeat is what you want:

我相信你想要的是重复:

c = repeat(b, a)

#1


5  

I believe repeat is what you want:

我相信你想要的是重复:

c = repeat(b, a)