w3resource
ankara escort
malatya escort
NumPy Exercises

NumPy: Construct an array by repeating

NumPy: Array Object Exercise-25 with Solution

Write a NumPy program to construct an array by repeating.
Sample array: [1, 2, 3, 4]

Pictorial Presentation:

NumPy: Construct an array by repeating

Sample Solution:-

Python Code:

import numpy as np
a = [1, 2, 3, 4]
print("Original array")
print(a)
print("Repeating 2 times")
x = np.tile(a, 2)
print(x)
print("Repeating 3 times")
x = np.tile(a, 3)
print(x)

Sample Output:

Original array                                                         
[1, 2, 3, 4]                                                           
Repeating 2 times                                                      
[1 2 3 4 1 2 3 4]                                                      
Repeating 3 times                                                      
[1 2 3 4 1 2 3 4 1 2 3 4]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to test whether any array element along a given axis evaluates to True.
Next: Write a NumPy program to repeat elements of an array.

What is the difficulty level of this exercise?



New Exercises:R Programming