如何将数组复制到特定长度的数组 [英] How to replicate array to specific length array

查看:345
本文介绍了如何将数组复制到特定长度的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个小数组复制到特定长度的数组

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆