'expand 1d numpy array

I want to expand/repeat each element in a 1D array for different times

x = array([7,1,9])  #1d array
rep_size = array([3,2,2]) # repeat number for each element in x
result = arary([7,7,7,1,1,9,9]) #expected result

Is there a numpy function can do this if I don't want to use a for loop. Thanks.



Solution 1:[1]

Use numpy.repeat:

result = np.repeat(x, rep_size)

output: array([7, 7, 7, 1, 1, 9, 9])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway