Professional Documents
Culture Documents
Lecture No 9
Lecture No 9
Lecture No 9
Two dimensional array is an array within an array. It is an array of arrays. In this type of array the
position of an data element is referred by two indices instead of one. So it represents a table with
rows and columns of data. In the below example of a two dimensional array, observer that each
array element itself is also an array.
Consider the example of recording temperatures 4 times a day, every day. Some times the
recording instrument may be faulty and we fail to record data. Such data for 4 days can be
presented as a two dimensional array as below.
Day 1 - 11 12 5 2
Day 2 - 15 6 10
Day 3 - 10 8 12 5
Day 4 - 12 15 8 6
1
Vida Statkevičienė. Fundamentals of programming. Lecture No. 9. (VMU IF)
Inserting values in two dimensional array
We can insert new data elements at specific position by using the insert() method and specifying
the index.
In the below example a new data element is inserted at index position 2.
del T[3]
for r in T:
for c in r:
print(c,end = " ")
print()
2
Vida Statkevičienė. Fundamentals of programming. Lecture No. 9. (VMU IF)
When the above code is executed, it produces the following result −
11 12 5 2
15 6 10
10 8 12 5
>>>matr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>>print(matr)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> arr1 = [11,12,13]
>>> arr2 = [21,22,23]
>>> arr3 = [31,32,33]
>>> arr4 = [41,42,43]
>>> matr2 = [arr1, arr2, arr3]
>>>print(matr2)
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
>>>matr2.append(arr4)
>>>print(matr2)
[[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]]
>>>matr2[2]
[31, 32, 33]
>>>matr2[2][1]
32
>>>matr2[2][1] = 555
>>>print(matr2)
[[11, 12, 13], [21, 22, 23], [31, 555, 33], [41, 42, 43]]
>>> matr = [[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]]
>>> print(matr)
[[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]]
>>> print("sum 1=", sum(matr[1]))
sum 1= 66
>>> print("sum 0=", sum(matr[0]))
sum 0= 36
>>> print("sum 2=", sum(matr[2]))
sum 2= 96
>>> print("sum 3=", sum(matr[3]))
sum 3= 126
3
Vida Statkevičienė. Fundamentals of programming. Lecture No. 9. (VMU IF)
and average:
>>> print("average 0=", sum(matr[0])/len(matr[0]))
average 0= 12.0
>>> print("average 1=", sum(matr[1])/len(matr[1]))
average 1= 22.0
and average:
>>> print("average column 0=", (matr[0][0] + matr[1][0] + matr[2][0] +
matr[3][0])/len(matr))
Average column 0= 26.0
Try it:
number of rows, m = 2
number of columns, n = 3
entry in row: 1 column: 1
7
entry in row: 1 column: 2
5
entry in row: 1 column: 3
6
entry in row: 2 column: 1
2
entry in row: 2 column: 2
6
entry in row: 2 column: 3
5
[[7, 5, 6], [2, 6, 5]]
4
Vida Statkevičienė. Fundamentals of programming. Lecture No. 9. (VMU IF)
Entering numbers into two dimensional array – matrix. Example No. 2:
matr = []
n = int(input("Enter number of lines "))
m = int(input("Enter number of colums "))
for i in range(n):
row = []
for j in range(m):
v = int(input("Enter value of " + str(i) + " row and " + str(j) + " column "))
row.append(v)
matr.append(row)
print(matr)
Entering numbers into two dimensional array – matrix FROM DATA FILE:
matr = []
f = open("a.txt")
for line in f:
e = list(map(int,line.split()))
matr.append(e)
print(matr)
5
Vida Statkevičienė. Fundamentals of programming. Lecture No. 9. (VMU IF)