1

I try do generate a series of x and repeat it n times:

import numpy as np
​
x = [1,2,3,4]
n = [1,2,3,4]
​
data = []
​
for i,j in zip(x, n):
        data.append(np.repeat(i,j))

print(data)

Out:

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

I want a "clean" array like:

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

Thanks a lot if someone could point me to a solution!

Tyrus Rechs
  • 73
  • 2
  • 12

2 Answers2

1

You can use np.concatenate to concatenate a list of array along an axis (here the first axis).

data = np.concatenate(data)

does what you want.

Anis
  • 2,984
  • 17
  • 21
1

This can be done using a simple one-liner using np.concatenate:

data = np.concatenate([np.repeat(i, j) for i, j in zip(x, n)])
rdb
  • 1,488
  • 1
  • 14
  • 24