Multidimensional Arrays in Python
Multidimensional Arrays in Python
Rm 1 Rm 2 Rm 3 Rm 4 Rm 5
One Dimension
My room is 02-05
Hotel (2D)
4F 1 2 3 4 5 6
3F 1 2 3 4 5 6
2F 1 2 3 4 5 6
1F 1 2 3 4 5 6
Chess Position
• Row 8
• Col B
• So, B8
Dimensions
One Dimensional Array, A Two Dimensional Array, M
Index Contents
0 1 2 3
0 ‘Apple’
0 ‘Apple’ ‘Lah’ ‘Cat’ ‘Eve’
1 ‘John’
1 ‘Hello’ ‘Pay’ ‘TV’ ‘Carl’
2 ‘Eve’
2 ‘What’ ‘Bank’ ‘Radio’ ‘Ada’
3 ‘Mary’
3 ‘Frog’ ‘Peter’ ‘Sea’ ‘Eat’
4 ‘Ian’
Column index
Row index
• Equivalent to
1 2
3 4
What is the Difference Between …
• A list of lists • 2D Array
– More specifically, all the – Is a subset of a list of lists
lists do not have to be #rows,
the same length Can be any length
Length = 3 Can be any length, but
Length = 3 all the lists have to be
the same length
[[1,2],[3,4,5],[6]] [[1,2],[3,4],[5,6]]
Length = 2 Length = 3 Length = 1 Length = 2 Length = 2 Length = 2
↕
1 2
3 4
A Larger 2-d Array
• Creating a 4 x 10 matrix
pprint() from
the package
pprint makes
the format nicer
Number of rows
Number of columns
To Create an N x N Identical Matrix
2D Looping
• Let’s say we just want to print 0 and 1
2D Looping
• Let’s say we just want to print 0 and 1
j=0123456789
↓↓↓↓↓↓↓↓↓↓
i=0→
i=1→
i=2→
i=3→
i=4→
i=5→
i=6→
i=7→
i=8→
i=9→
To Create an R x C Zero Matrix
Accessing Entries
• Remember
– Frist row index
– Then column index
2D Array Applications
• Matrix operations
• Recoding 2D data
– Tables
– Spatial Data
Matrix Addition
• How do to Matrix
Multiplication or
other matrix
operations, e.g.
inverse, transpose?
Tables
• Just like other Spreadsheet applications
Name Stu. No. English Math Science Social Studies
John A1000000A 90 80 100 70
Peter A1000009D 60 100 60 90
Paul A1000003C 80 80 70 90
Mary A1000001B 100 70 80 80
Tables
• Like in Spreadsheet applications
Name Stu. No. English Math Science Social Studies
John A1000000A 90 80 100 70
Peter A1000009D 60 100 60 90
Paul A1000003C 80 80 70 90
Mary A1000001B 100 70 80 80
• Challenge
– Given the row and column indices i and j, how to
compute the position pos?
A Simple TTT Game
• First, for a matrix game that represent the
current state of the game, let’s make a
function to print it nicely
A Simple TTT Game
Create the game
board
A Simple TTT Game
Put the number 1 to
9 into the board for
display purposes
A Simple TTT Game
There are two players,
Player 0 and Player 1
Player 0 uses ‘X’
Player 1 uses ‘O’
[100,20
Red, green, blue
0,100] = [100,200,100]
2+1 = 3D Array as a Picture
One pixel is a list
of three values of
red, blue and
green.
Usually ranging
from 0 to 255
imshow() is to
draw a picture in
bitmap
• 3 x 3 pixels (zoomed)
Image Processing
Using the imagio package to read in a photo as a 3D
Negative Image
Negative Image
imshow() is to
draw a picture in
bitmap
• 3 x 3 pixels (zoomed)
Dimensions
One Dimensional Array, A Two Dimensional Array, M
Index Contents
0 1 2 3
0 ‘Apple’
0 ‘Apple’ ‘Lah’ ‘Cat’ ‘Eve’
1 ‘John’
1 ‘Hello’ ‘Pay’ ‘TV’ ‘Carl’
2 ‘Eve’
2 ‘What’ ‘Bank’ ‘Radio’ ‘Ada’
3 ‘Mary’
3 ‘Frog’ ‘Peter’ ‘Sea’ ‘Eat’
4 ‘Ian’
Remember these
four lines of code