问题
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.
回答1:
Use np.tile
>>> a = np.array([True, True, False])
>>> np.tile(a, 3)
... array([ True, True, False, True, True, False, True, True, False])
回答2:
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]
来源:https://stackoverflow.com/questions/55889956/repeat-ndarray-n-times