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
干杯,迈克