Repeat ndarray n times [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-07-04 03:04:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!