0% found this document useful (0 votes)
8 views

Multidimensional Arrays in Python

Intro to python, multidimensional array construction

Uploaded by

Sri Dhanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Multidimensional Arrays in Python

Intro to python, multidimensional array construction

Uploaded by

Sri Dhanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Dimensions

• Are we living in a three dimensional space?

The xkcd guide to the fourth dimension


Two-dimensional Array
Motel in US (1D)

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’

5 ‘Smith’ 4 ‘Job’ ‘Fry’ ‘Gym’ ‘Wow’

6 ‘Kelvin’ 5 ‘Walk’ ‘Fly’ ‘Cook’ ‘Look’

A[5] = ‘Smith’ M[4][1] = ‘Fry’


2-Dimensional Array in Python
• 1-d array • 2-d array

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

• Or, you can setup a more “comprehensible”


column index dictionary
Tables
• You can even append a new record for a new
student
Tables
• Or do any computations on the table
– E.g. The average of the English scores
Spatial Data

Tic-tac-toe, Chess, SRPG


Tic-tac-toe
• Tic-tac-toe
– A game of 3 x 3 grid
– How to represent the game?
• A 3 x 3 Matrix:
game = createZeroMatrix(3,3)
• How to ”make a move”?
– You can directly ask the user to input a pair of
coordinates, e.g. (1,1)
– However, sometime it’s confusing for users who
cannot tell row indices from column indices
Tic-tac-toe
• Because the grid size is very small, we can
make it easier for the user by labeling each
box with a number from 1 to 9
• For the position pos from 1 to 9, we can map
to the coordinates (i,j) as
i = (pos-1)//3
j = (pos-1)%3

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

The first player is Player 0

And they will take turns


A Simple TTT Game
Anyhow play 9 times
(We didn’t implement any
error or winning checking
yet)
Gameplay
Chess
Other Text Based Chess
Chinese Checkers?
• How to represent the board?
3D Chess
Other 2D Array Games
RPG, SRPG
• Virus Riddle
circlePic()
ASCII Arts
Wait…
• What if I put three colors as each of the item
in my 2D array?

[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

The picture has h x w numbers of pixels


(h = #rows, w = #columns)
Loop through each pixel with indices (i,j)
Image Processing
Dyeing Hair

Loop through each pixel with indices (i,j)

If pic[i][j] has more green, change it to purple


Multi-dimensional Array
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)
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’

5 ‘Smith’ 4 ‘Job’ ‘Fry’ ‘Gym’ ‘Wow’

6 ‘Kelvin’ 5 ‘Walk’ ‘Fly’ ‘Cook’ ‘Look’

A[5] = ‘Smith’ M[4][1] = ‘Fry’


Multi-Dimensional Array
• A three dimensional array
Fancy Terminology in Business
• Multidimensional Data Model
• Multidimensional Analysis
Sales Cube
• Product Type x Year x Country

• 4th Dimensional Sales Cube:


– Product Type x Year x Country x {Predicted vs
actual}
Multi-Dimensional Array
• Year x Ethnic Group x {death/birth}
2D Array x CSV
• Remember last time, we plotted the Singapore
birth rate with the data in a CSV file
Read in a Reading CSV Files Create a CSV File
CSV file Reader
into a list

Remember these
four lines of code

No need for all


those string
strip(), split() etc.
Caution
• Again, using lists or tuples?
• Again, the old list referencing problem

You might also like