Create array X with certain number of copies of each element in array Y in python

Viewed 124

I have an array counts = [2, 3, 4] with each element denoted by c. I want to convert it into an array weights that has m copies of each element c, where m is an element in M = [3, 2, 1].

So in the end, I'd like an array weights = [2, 2, 2, 3, 3, 4]

Is there an efficient way to do this in numpy?

1 Answers

numpy.repeat does exactly what you want:

>>> np.repeat(counts, M)
array([2, 2, 2, 3, 3, 4])
Related