Create an array with same element repeated multiple times
You can do it like this:
function fillArray(value, len) {
if (len == 0) return [];
var a = [value];
while (a.length * 2 <= len) a = a.concat(a);
if (a.length < len) a = a.concat(a.slice(0, len - a.length));
return a;
}
It doubles the array in each iteration, so it can create a really large array with few iterations.
Note: You can also improve your function a lot by using push
instead of concat
, as concat
will create a new array each iteration. Like this (shown just as an example of how you can work with arrays):
function fillArray(value, len) {
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(value);
}
return arr;
}
How to 'repeat' an array n times
You could do this:
var repeated = [].concat(... new Array(100).fill([1, 2, 3]));
That creates an array of a given length (100 here) and fills it with the array to be repeated ([1, 2, 3]
). That array is then spread as the argument list to [].concat()
.
Oh wait just
var repeated = new Array(100).fill([1, 2, 3]).flat();
would be a little shorter.
repeating a numpy array N times
This is what you want:
x=np.concatenate([my_input_array, my_input_array, my_input_array])
for i in x:
print(i)
Output:
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
Repeat a 2D NumPy array N times
By my tests, np.repeat
is a little faster than np.tile
:
X = np.repeat(arr[None,:], 3, axis=0)
Alternatively, use np.concatenate
:
X = np.concatenate([[arr]] * 3, axis=0)
arr = np.arange(10000 * 1000).reshape(10000, 1000)
%timeit np.repeat(arr[None,:], 3, axis=0)
%timeit np.tile(arr, (3, 1, 1))
%timeit np.concatenate([[arr]] * 3, axis=0)
# Read-only, array cannot be modified.
%timeit np.broadcast_to(arr, (3, *arr.shape))
# Creating copy of the above.
%timeit np.broadcast_to(arr, (3, *arr.shape)).copy()
170 ms ± 3.82 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
187 ms ± 3.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
243 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
10.9 µs ± 218 ns per loop (mean ± std. dev. of 7 runs, 100000 loops
189 ms ± 2.45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)each)
np.array_equals(np.repeat(arr[None,:], 3, axis=0),
np.tile(arr, (3, 1, 1))
True
Creating a list repeated n times using an existing variable which is mutable
You can just use .copy()
:
import numpy as np
R = np.array([[2,3,0], [6,78,8],[1,2,3]])
U = [R.copy() for _ in range(3)]
U[1][2,2] = 0
print(U)
Gives:
[array([[ 2, 3, 0],
[ 6, 78, 8],
[ 1, 2, 3]]),
array([[ 2, 3, 0],
[ 6, 78, 8],
[ 1, 2, 0]]),
array([[ 2, 3, 0],
[ 6, 78, 8],
[ 1, 2, 3]])]
Repeat ndarray n times
Use np.tile
>>> a = np.array([True, True, False])
>>> np.tile(a, 3)
... array([ True, True, False, True, True, False, True, True, False])
Python: How to fill an array with x repeated n times?
This can be done using a simple one-liner using np.concatenate
:
data = np.concatenate([np.repeat(i, j) for i, j in zip(x, n)])
Related Topics
How to Convert Np.Int64 into Python Int64 for Pandasseries
Valueerror: Too Many Values to Unpack (Expected 2) in Django
Use Python Extract Images from Excel Sheets
Split a String At Uppercase Letters
Check Type: How to Check If Something Is a Rdd or a Dataframe
Incorrect Column Alignment When Printing Table in Python Using Tab Characters
Fastest 2D Convolution or Image Filter in Python
Iterate Through a List by Skipping Every 5Th Element
Implement K-Fold Cross Validation in Mlpclassification Python
Issue Skipping Song by Requester
How to Manage Division of Huge Numbers in Python
Split List into Two Parts Based on Some Delimiter in Each List Element in Python
Construct Networkx Graph from Pandas Dataframe
Pandas To_Csv() Slow Saving Large Dataframe
Comparing Two Xml Files in Python