...

Web Technologies - NumPy

Back to Course

Lesson Description


Lession - #301 NumPy Array Iterating


Iterating Arrays

Iterating means going through elements one by one.
As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
If we iterate on a 1-D array it will go through each element one by one.

Example
Iterate on the elements of the following 1-D array:
import numpy as np

arr = np.array([1, 2, 3]>
for x in arr: print(x>


Iterating 2-D Arrays

In a 2-D array it will go through every one of the lines.

Example
Iterate on the components of the accompanying 2-D array:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]]>
for x in arr: print(x>


Iterating 3-D Arrays

In a 3-dimensional array it will go through every one of the 2-D arrays.

Example
Iterate on the components of the accompanying three dimensional array :
import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]>
for x in arr: print(x>


Iterating Arrays Using nditer(>

The function nditer(>
is an assisting function that with canning be utilized from exceptionally essential to extremely progressed iterations. It tackles a few fundamental issues which we face in iteration, gives up through it with examples.

Iterating on Each Scalar Element
In essential for loops, iterating through every scalar of an array we want to involve n for loops which can be hard to compose for arrays with extremely high dimensionality.
import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]>
for x in np.nditer(arr>
: print(x>


Iterating Array With Different Data Types

We can utilize op_dtypes argument and pass it the normal datatype to change the datatype of components while iterating.
NumPy doesn't change the data type of the component set up (where the component is in array>
so it needs another space to play out this activity, that additional room is called buffer, and to empower it in nditer(>
we pass flags=['buffered'].

Example
Iterate through the array as a string:
import numpy as np

arr = np.array([1, 2, 3]>
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']>
: print(x>


Enumerated Iteration Using ndenumerate(>

Enumeration implies referencing arrangement number of somethings individually.
Some of the time we require comparing index of the component while iterating, the ndenumerate(>
strategy can be utilized for those usecases.

Example
Enumerate on following 1D arrays components:
import numpy as np

arr = np.array([1, 2, 3]>
for idx, x in np.ndenumerate(arr>
: print(idx, x>








This python numpy tutorial blog includes all the basics of Python, its various operations, special functions
How to take array input in python? ; a=[] ; n=int(input("Number of elements in array:">
>
; for i in range(0,n>
: ; l=int(input(>
>
; a. · append(l>
.
We can create matrix Python using a nested list.
Introducing NumPy and Pandas ... NumPy is a library for Python that adds support for large, multi-dimensional arrays and matrices, along with a large collection .
Python Array Example – Declare, Append, Index, Remove, Count · 'b' -> Represents signed integer of size 1 byte · 'B' -> Represents unsigned .