w3resource

NumPy: Find the index of the sliced elements as follows from a given 4x4 array

NumPy: Array Object Exercise-122 with Solution

Write a NumPy program to find the index of the sliced elements as follows from a given 4x4 array.

Pictorial Presentation:

NumPy: Find the index of the sliced elements as follows from a given 4x4 array

Sample Solution:

Python Code:

import numpy as np    
x = np.reshape(np.arange(16),(4,4))
print("Original arrays:")
print(x)
print("Sliced elements:")
result = x[[0,1,2],[0,1,3]]
print(result)

Sample Output:

Original arrays:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
Sliced elements:
[ 0  5 11]

Python Code Editor:

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

Previous: Write a NumPy program to join a sequence of arrays along a new axis.
Next: Write a NumPy program to create two arrays when the size is bigger or smaller than a given 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}