w3resource

NumPy: Create an array which looks like below array

NumPy: Array Object Exercise-48 with Solution

Write a NumPy program to create an array which looks like below array.

Sample Solution:-

Python Code:

import numpy as np
x = np.triu(np.arange(2, 14).reshape(4, 3), -1)
print(x)

Sample Output:

[[ 2  3  4]                                                            
 [ 5  6  7]                                                            
 [ 0  9 10]                                                            
 [ 0  0 13]]

Python Code Editor:

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

Previous: Write a NumPy program to create an array which looks like below array.
Next: Write a NumPy program to collapse a 3-D array into one dimension array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}