39. Numpy 2D Array using zeros and ones Function
39. Numpy 2D Array using zeros and ones Function
print(a[1][1])
print(a[2][0])
print(a[2][1])
Accessing 2D Array using for loop
from numpy import *
a = array([ [0., 0.],
a = zeros((3, 2)) [0., 0.],
[0., 0.] ])
The outer for loop represents the rows and the inner for loop represents the columns in
each row.
Accessing 2D Array using while loop
from numpy import *
a = array([ [0., 0.],
a = zeros((3, 2)) [0., 0.],
n = len(a) [0., 0.] ])
i=0
while i < n :
j=0
while j < len(a[i]):
print(a[i][j])
j+=1
i+=1
ones( ) Function
ones ( ) Function is used to create 2D array with several rows and columns with all 1s.
Syntax:-
numpy.ones(shape, dtype=float, order='C')
Where,
shape – shape of new array. It can be an int which will represent number of elements or can be
tuple of int. ex:- (3, 2)
rows columns
dtype – The desired data-type for the array. Default is float.
print(a[1][1])
print(a[2][0])
print(a[2][1])
Accessing 2D Array using for loop
from numpy import *
a = array([ [1., 1.],
a = ones((3, 2)) [1., 1.],
[1., 1.] ])
The outer for loop represents the rows and the inner for loop represents the columns in
each row.
Accessing 2D Array using while loop
from numpy import *
a = array([ [1., 1.],
a = ones((3, 2)) [1., 1.],
n = len(a) [1., 1.] ])
i=0
while i < n :
j=0
while j < len(a[i]):
print(a[i][j])
j+=1
i+=1