import numpy as np
a = np.random.randint(0, 10, 8); print(a);
for i in a:
print(i);
np.shape(a)
np.size(a)
for i in np.arange(0, np.size(a)): # np.size(a) = 8
print("a[", i, "] = ", a[i]);
np.arange(0, np.size(a))
b = np.random.randint(0,10, size=(4,5)); print(b);
for i in b:
print("b row: ", i);
# This is a nested loop
for i in b: # Loops over all the rows
for j in i: # Loops over all the elements of a row
print(j);
for i in np.nditer(b):
print(i);
for x in np.nditer(b, op_flags = ['readwrite']):
x[...] = 2*x
print(x)
np.size(b)
np.shape(b)
print(a);
print(np.arange(0, np.size(a))); # generate linearly spaced integers and print them
for i in np.arange(0, np.size(a)): #notice how indentation works in conditional statements
if a[i] == 0:
break
else:
print(a[i]);
counter = 0;
for i in np.arange(0, np.size(a)):
if a[i] == 0:
counter = counter + 1;
print("zero encountered at location: ", i);
print("Total zeros encoutered =",counter);
#a = np.array([5, 4, 3, 2, 1]);
a = np.random.randint(0, 20, 7);
print(a);
print(np.size(a));
print(np.arange(1, np.size(a))) # np.arange(1, 5)
i = 1
print(np.arange(0, np.size(a)-i))
# Indentation is important: pressing tab
for i in np.arange(1, np.size(a)):
for j in np.arange(0, np.size(a)-i):
if a[j+1]<a[j]:
temp = a[j+1]
a[j+1] = a[j]
a[j] = temp
else:
print("No swap required")
print(j, a)
print(a);
b = np.sort(a);
print(b);
c = np.random.randint(0, 16, size=(4,4))
print(c)
d = np.sort(c,axis =1)
print("Sorted Matrix column-wise")
print(d)
d = np.sort(c,axis =0)
print("Sorted Matrix row-wise")
print(d)