CS 111: Program Design I

11/22/16

CS 111: Program Design I

Lecture #24: 2D lists, heat maps, Predictive Policing

Robert H. Sloan & Richard Warner University of Illinois at Chicago Date

Important Announcement

n Turkey special: TA Office Hours TODAY Tuesday, 5:00?6:30 pm, 4030 SEL (Ellen)

n No office hours Wednesday afternoon?Friday of this week (Thanksgiving)

n Amanda may schedule one hour late Wednesday morning if her class schedule permits

1

B = [[1,2,3], [5,10,20]] print(B[1])

This will print

Clicker A B C D E

2 [1,2,3] [5,10,20] This will cause an error I don't know

11/22/16

How confident are you of your answer?

A. Very Highly confident: I've got this B. Very confident C. Somewhat confident D. Not so confident: educated guess E. Not confident at all: random guess and/or

bullied into by the rest of my small group

2

Matrix

n Famous 1999 Fantasy/Action movie about Neo and the elusive Morpheus

n Way some students believe that they can learn Computer Science: By plugging themselves into it

11/22/16

Matrix

n Famous 1999 Fantasy/Action movie about Neo and the elusive Morpheus

n Way some students believe that they can learn Computer Science: By plugging themselves into it

n Rectangular array of (usually) numbers, e.g.,

3

Matrices in Python

n Two ways to represent:

q Usually: For m-by-n matrix, list of m lists, where each inner nested list is of same length (n) and represents one row

q (Can also use numpy module)

11/22/16

Creating nested list

n Literal notation:

matrix = [ [5, 10, 15, 20, 25], [30, 35, 40, 45, 50], [55, 60, 65, 70, 75], [80, 85, 90, 95, 100], [105, 110, 115, 120, 125]

]

4

11/22/16

Building up nested list

n Create distinct list of desired row or row of 0s to change later for each row append in:

matrix = [ ] for row in range(number_rows):

new_row = [ ] for col in range(number_cols):

new_row.append(0) #if starting all-0 matrix.append(new_row)

Useful function

def make_0array(nrows, ncols):

'''returns new nrows x ncols 2-d list/array of all 0s'''

array = [ ] # Build up array of numbers here

for j in range(nrows):

new_blank_row = [ ]

# Make a NEW row

for i in range(ncols):

new_blank_row.append(0)

array.append(new_blank_row)

return array

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download