[go: up one dir, main page]

0% found this document useful (0 votes)
26 views5 pages

Lecture No 9

A two dimensional array, also known as a matrix, is an array of arrays that allows data to be stored and accessed using two indices. Elements in a 2D array can represent data organized into rows and columns, such as temperatures recorded each day. Values in a 2D array can be accessed, inserted, updated, and deleted using their indices. Matrices are a special type of 2D array where each element is the same size, making them useful for mathematical calculations.

Uploaded by

Farid Babayev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Lecture No 9

A two dimensional array, also known as a matrix, is an array of arrays that allows data to be stored and accessed using two indices. Elements in a 2D array can represent data organized into rows and columns, such as temperatures recorded each day. Values in a 2D array can be accessed, inserted, updated, and deleted using their indices. Matrices are a special type of 2D array where each element is the same size, making them useful for mathematical calculations.

Uploaded by

Farid Babayev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Two dimensional array in Python.

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

The above data can be represented as a two dimensional array as below.


T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

Accessing values in a two dimensional array


The data elements in two dimesnional arrays can be accessed using two indices. One index
referring to the main or parent array and another index referring to the position of the data element
in the inner array. If we mention only one index then the entire inner array is printed for that index
position. The example below illustrates how it works.

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]


print(T[0])
print(T[1][2])
When the above code is executed, it produces the following result −
[11, 12, 5, 2]
10
To print out the entire two dimensional array we can use python for loop as shown below. We use
end of line to print out the values in different rows.

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]


for r in T:
for c in r:
print(c,end = " ")
print()

When the above code is executed, it produces the following result −


11 12 5 2
15 6 10
10 8 12 5
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.

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]


T.insert(2, [0,5,11,13,6])
for r in T:
for c in r:
print(c,end = " ")
print()

When the above code is executed, it produces the following result −


11 12 5 2
15 6 10
0 5 11 13 6
10 8 12 5
12 15 8 6
Updating values in two dimensional array
We can update the entire inner array or some specific data elements of the inner array by
reassigning the values using the array index.
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
T[2] = [11,9]
T[0][3] = 7
for r in T:
for c in r:
print(c,end = " ")
print()

When the above code is executed, it produces the following result −


11 12 5 7
15 6 10
11 9
12 15 8 6
Deleting the values in two dimensional array
We can delete the entire inner array or some specific data elements of the inner array by
reassigning the values using the del() method with index. But in case you need to remove specific
data elements in one of the inner arrays, then use the update process described above.
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

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

Two dimensional array - matrix:


Matrix is a special case of two dimensional array where each data element is of strictly same size.
So every matrix is also a two dimensional array but not vice versa. Matrices are very important data
structures for many mathematical and scientific calculations.

>>>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]]

Sum of each row:

>>> 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

The sum of column 0:


>>> print("sum column 0=", matr[0][0] + matr[1][0] + matr[2][0] + matr[3][0])
sum column 0= 104

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

Entering numbers into two dimensional array – matrix. Example No. 1:


m = int(input('number of rows, m = '))
n = int(input('number of columns, n = '))
matrix = []
# initialize the number of rows
for i in range(0,m):
matrix += [0]
# initialize the matrix
for i in range (0,m):
matrix[i] = [0]*n
# enter data
for i in range (0,m):
for j in range (0,n):
print ('entry in row: ',i+1,' column: ',j+1)
matrix[i][j] = int(input())
print (matrix)

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)

You might also like