2

I have a numpy.ndarray with True/False:

import numpy as np    
a = np.array([True, True, False])

I want:

out = np.array([True, True, False, True, True, False, True, True, False])

I tried:

np.repeat(a, 3, axis = 0)

But it duplicates each element, I want to duplicate the all array.

This is the closes I got:

np.array([a for i in range(3)])

However, I want it to stay as 1D.

Edit

It was suggested to be a duplicate of Repeating each element of a numpy array 5 times. However, my question was how to repeat the all array and not each element.

Community
  • 1
  • 1
DJV
  • 4,743
  • 3
  • 19
  • 34

2 Answers2

4

Use np.tile

>>> a = np.array([True, True, False])
>>> np.tile(a, 3)
... array([ True,  True, False,  True,  True, False,  True,  True, False])
nick
  • 1,310
  • 8
  • 15
1

Try:

import numpy as np
a = np.array([True, True, False])
print(np.concatenate([a]*3))

[ True  True False  True  True False  True  True False]
giser_yugang
  • 6,058
  • 4
  • 21
  • 44