- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set a 1-D iterator over a Numpy array
For a 1-D iterator over the array, use the numpy.flat() method in Python Numpy. This is a numpy.flatiter instance, which acts similarly to, but is not a subclass of, Python’s built-in iterator object.
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
Steps
At first, import the required library −
import numpy as np
Create a 2d array −
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
For a 1-D iterator over the array, use the numpy.flat() method in Python Numpy −
print("
Result...
",arr.flat[2]) print("
Result...
",arr.flat[7]) print("
Result...
",arr.flat[9])
Get the type of the flat iterator −
print("
Get the type...
",type(arr.flat))
Example
import numpy as np # Create a 2d array arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69], [69, 80, 80, 99]]) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # For a a 1-D iterator over the array, use the numpy.flat() method in Python Numpy print("
Result...
",arr.flat[2]) print("
Result...
",arr.flat[7]) print("
Result...
",arr.flat[9]) # Get the type of the flat iterator print("
Get the type...
",type(arr.flat))
Output
Array... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16 Result... 78 Result... 45 Result... 67 Get the type... <class 'numpy.flatiter'>
- Related Articles
- Numpy Array advantage over a Nested List
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over axis 1
- Stack 1-D arrays as columns into a 2-D array in Numpy
- Expand the shape of an array over axis 1 in Numpy
- Return all the non-masked data as a 1-D array in Numpy
- Join a sequence of Numpy arrays with stack() over axis 1
- Compute the maximum of the masked array elements over axis 1 in Numpy
- Compute the minimum of the masked array elements over axis 1 in Numpy
- Construct an array by executing a function over each coordinate in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over specific axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- Return a list of slices corresponding to the masked clumps of a 1-D array in Numpy
- Return a list of slices corresponding to the unmasked clumps of a 1-D array in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over negative axis
- Pack the elements of a binary-valued array into bits in a uint8 array over specific axis in Numpy
