如何将数组复制到特定长度的数组 [英] How to replicate array to specific length array
本文介绍了如何将数组复制到特定长度的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个小数组复制到特定长度的数组
I want replicate a small array to specific length array
示例:
var = [22,33,44,55] # ==> len(var) = 4
n = 13
我想要的新数组是:
var_new = [22,33,44,55,22,33,44,55,22,33,44,55,22]
这是我的代码:
import numpy as np
var = [22,33,44,55]
di = np.arange(13)
var_new = np.empty(13)
var_new[di] = var
我收到错误消息:
DeprecationWarning:分配将在将来引发错误,最有可能是因为您的索引结果形状与值数组形状不匹配.您可以使用
arr.flat[index] = values
保留旧的行为.
但是我得到了相应的变量:
But I get my corresponding variable:
var_new
array([ 22., 33., 44., 55., 22., 33., 44., 55., 22., 33., 44.,
55., 22.])
那么,如何解决错误?有其他选择吗?
So, how to solve the error? Is there an alternative?
推荐答案
There are better ways to replicate the array, for example you could simply use np.resize
:
返回具有指定形状的新数组.
Return a new array with the specified shape.
如果新数组大于原始数组,则新数组将填充a
的重复副本. [...]
If the new array is larger than the original array, then the new array is filled with repeated copies of a
. [...]
>>> import numpy as np
>>> var = [22,33,44,55]
>>> n = 13
>>> np.resize(var, n)
array([22, 33, 44, 55, 22, 33, 44, 55, 22, 33, 44, 55, 22])
这篇关于如何将数组复制到特定长度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文