Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate array to specific length array

I want replicate a small array to specific length array

Example:

var = [22,33,44,55] # ==> len(var) = 4
n = 13

The new array that I want would be:

var_new = [22,33,44,55,22,33,44,55,22,33,44,55,22]

This is my code:

import numpy as np
var = [22,33,44,55]
di = np.arange(13)
var_new = np.empty(13)
var_new[di] = var

I get error message:

DeprecationWarning: assignment will raise an error in the future, most likely because your index result shape does not match the value array shape. You can use arr.flat[index] = values to keep the old behaviour.

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?

like image 424
delta27 Avatar asked Jul 16 '17 11:07

delta27


People also ask

How to create an array of certain length in JavaScript?

One common way of creating an Array with a given length, is to use the Array constructor: const LEN = 3; const arr = new Array(LEN); assert. equal(arr. length, LEN); // arr only has holes in it assert.

How does array length work?

Summary. The length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index of the array. The length returns the number of elements that a dense array has. For the spare array, the length doesn't reflect the number of actual elements in the array.

What does array length return in JavaScript?

The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

How do you resize an array in Python?

With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.


1 Answers

There are better ways to replicate the array, for example you could simply use np.resize:

Return a new array with the specified shape.

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])
like image 143
MSeifert Avatar answered Oct 19 '22 18:10

MSeifert