简体   繁体   English

numpy数组扩展的特定方式

[英]specific way of numpy array expansion

I have the following numpy array (as an example): 我有以下numpy数组(作为示例):

[0,1,5,4,3]

Is there any way to "repeat" this array, but in a specific way: I need a final array of shape 25 but arranged in a way that firs I would have 5 zeros, then- 5 ones, then- 5 fives, etc. Example of desired output: 有什么办法可以“重复”这个数组,但是有一种特定的方式:我需要一个形状为25的最终数组,但排列方式要使firs我有5个0,然后-5个,然后-5个5,依此类推。所需输出示例:

[0,0,0,0,0,1,1,1,1,1,
 5,5,5,5,5,4,4,4,4,4
 3,3,3,3,3]

if I do np.append(arr, arr) - it will give me two initial sequenced arrays, and this is not my goal BTW, my real array is not sorted and should remain unsorted in the end. 如果我执行np.append(arr, arr) -它会给我两个初始的序列数组,这不是我的目标,顺便说一句,我的真实数组未排序,最后应该保持未排序状态。

Thanks in advance! 提前致谢!

Use np.repeat . 使用np.repeat

>>> np.repeat([0,1,5,4,3], 5)
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3,
       3, 3, 3])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM