w3resource

NumPy: Create a 2-dimensional array of size 2 x 3, composed of 4-byte integer elements

NumPy: Array Object Exercise-170 with Solution

Create a 2-dimensional array of size 2 x 3, composed of 4-byte integer elements. Write a NumPy program to find the number of occurrences of a sequence in the said array.

Sample Solution:

Python Code:

import numpy as np
np_array = np.array([[1, 2, 3], [2, 1, 2]], np.int32)
print("Original Numpy array:")
print(np_array)
print("Type: ",type(np_array))
print("Sequence: 1,2",)
result = repr(np_array).count("1, 2")
print("Number of occurrences of the said sequence:",result)

Sample Output:

Original Numpy array:
[[1 2 3]
 [2 1 2]]
Type:  <class 'numpy.ndarray'>
Sequence: 1,2
Number of occurrences of the said sequence: 2

Pictorial Presentation:

NumPy: Create a 2-dimensional array of size 2 x 3, composed of 4-byte integer elements.

Python Code Editor:

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

Previous: Write a NumPy program to get all 2D diagonals of a 3D NumPy array.
Next: Write a NumPy program to search the index of a given array in another 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}