Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van - - PowerPoint PPT Presentation
Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van - - PowerPoint PPT Presentation
CS 1110: Introduction to Computing Using Python Lecture 14 Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements Prelim = not Thursday or Friday might be on weekend or next week Dean of
- Prelim = not Thursday or Friday
- might be on weekend or next week
- Dean of Faculty supports our request to extend
the drop deadline (but no official decision yet)
Announcements
3/16/17 Nested Lists and Dictionaries 2
- Tuesday and Thursday: Recursion.
- Reading: 5.8-5.10
Next week: Recursion
3/16/17 Nested Lists and Dictionaries 3
Announcements: Lab 8
- Lab 8 has been released
- Could be useful as extra for loop practice.
- Prelim does not depend on any material
introduced in this lab.
3/16/17 Nested Lists and Dictionaries 4
Nested Lists
- Lists can hold any objects
- Lists are objects
- Therefore lists can hold other lists!
x = [1, [2, 1], [1, 4, [3, 1]], 5]
x[0] x[1][1] x[2][2][0] x[2][0] x[1] x[2] x[2][2]
b = [3, 1] c = [1, 4, b] a = [2, 1] x = [1, a, c, 5]
Nested Lists and Dictionaries 5 3/16/17
Two Dimensional Lists
Table of Data Images
Nested Lists and Dictionaries 6
5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 6 7 8 0 0 1 2 3 1 4 2 3
Store them as lists of lists (row-major order) d = [[5,4,7,3],[4,8,9,7],[5,1,2,3],[4,1,2,9],[6,7,8,0]]
0 1 2 3 4 5 6 7 8 9 101112 1 2 3 4 5 6 7 8 9 10 11 12
Each row, col has a value Each row, col has an RGB value
3/16/17
Overview of Two-Dimensional Lists
- Access value at row 3, col 2:
d[3][2]
- Assign value at row 3, col 2:
d[3][2] = 8
- Number of rows of d:
- len(d)
- Number of cols in row r of d:
- len(d[r])
Nested Lists and Dictionaries 7
5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 6 7 8 0 d 0 1 2 3 1 4 2 3
3/16/17
How Multidimensional Lists are Stored
- b = [[9, 6, 4], [5, 7, 7]]
- b holds id of a one-dimensional list
- Has len(b) elements
- b[i] holds id of a one-dimensional list
- Has len(b[i]) elements
Nested Lists and Dictionaries 8
id2
9 6 4
id3
5 7 7
id1 id2 id3 id1 b
9 6 4 5 7 7
3/16/17
1 1 2 1 2
Ragged Lists: Rows w/ Different Length
Nested Lists and Dictionaries 9
- b = [[17,13,19],[28,95]]
id2
17 13 19
id3
28 95
id1 id1 b id2 id3
3/16/17
1 1 2 1
Slices and Multidimensional Lists
- Only “top-level” list is copied.
- Contents of the list are not altered
- b = [[9, 6], [4, 5], [7, 7]]
Nested Lists and Dictionaries 10
id2
9 6
id1 id2 id3 id1
b
id4 id3
4 5
id4
7 7
x = b[:2]
id5
x
id5 id2 id3
3/16/17
1 1 1 1 1 2
Slices and Multidimensional Lists
- Create a nested list
>>> b = [[9,6],[4,5],[7,7]]
- Get a slice
>>> x = b[:2]
- Append to a row of x
>>> x[1].append(10)
- What is now in x?
Nested Lists and Dictionaries 11
A: [[9,6,10]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know
3/16/17
Slices and Multidimensional Lists
- Create a nested list
>>> b = [[9,6],[4,5],[7,7]]
- Get a slice
>>> x = b[:2]
- Append to a row of x
>>> x[1].append(10)
- x now has nested list
[[9, 6], [4, 5, 10]]
- What is now in b?
Nested Lists and Dictionaries 12
A: [[9,6],[4,5],[7,7]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know
3/16/17
Data Wrangling: Transpose
Nested Lists and Dictionaries 13
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
Data Wrangling: Transpose
Nested Lists and Dictionaries 14
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
Data Wrangling: Transpose
Nested Lists and Dictionaries 15
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
Data Wrangling: Transpose
Nested Lists and Dictionaries 16
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
Data Wrangling: Transpose
Nested Lists and Dictionaries 17
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
1 3 5 2 4 6
Data Wrangling: Transpose
Nested Lists and Dictionaries 18
1 2 3 4 5 6
3/16/17
1 3 5 2 4 6
Data Wrangling: Transpose
Nested Lists and Dictionaries 19
1 2 3 4 5 6
3/16/17
1 3 5 2 4 6
Data Wrangling: Transpose
Nested Lists and Dictionaries 20
1 2 3 4 5 6
3/16/17
1 3 5 2 4 6
Data Wrangling: Transpose
Nested Lists and Dictionaries 21
1 2 3 4 5 6
3/16/17
Data Wrangling: Transpose
def transpose(table): """Returns: copy of table with rows and columns swapped Precondition: table is a (non-ragged) 2d List""" numrows = len(table) numcols = len(table[0]) # All rows have same no. cols result = [] # Result accumulator for m in range(numcols): row = [] # Single row accumulator for n in range(numrows): row.append(table[n][m]) # Build up row result.append(row) # Add result to table return result
Nested Lists and Dictionaries 22
1 2 3 4 5 6 1 3 5 2 4 6
3/16/17
Data Wrangling
3/16/17 Nested Lists and Dictionaries 23
1 2 3 1 2 3
Dictionaries (Type dict)
Description
- List of key-value pairs
- Keys are unique
- Values need not be
- Example: net-ids
- net-ids are unique (a key)
- names need not be (values)
- js1 is John Smith (class ’13)
- js2 is John Smith (class ’16)
Python Syntax
- Create with format:
{k1:v1, k2:v2, …}
- Keys must be immutable
- ints, floats, bools, strings
- Not lists or custom objects
- Values can be anything
- Example:
d = {'ec1':'Ezra Cornell', 'ec2':'Ezra Cornell', 'ela63':'Erik Andersen'}
Nested Lists and Dictionaries 25 3/16/17
Using Dictionaries (Type dict)
- Access elts. like a list
- d['ec1'] evaluates to 'Ezra'
- But cannot slice ranges!
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}
Nested Lists and Dictionaries 26
'ela63' id8 'Ezra' 'Ezra' 'Erik' dict 'ec2' 'ec1' id8 d
3/16/17
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}
Nested Lists and Dictionaries 27
'ela63' id8 'Ezra' 'Ezra' 'Erik' dict 'ec2' 'ec1' id8 d
3/16/17
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
Nested Lists and Dictionaries 28
id8 dict id8 d
3/16/17
'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1'
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}
id8 dict
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
- Can add new keys
- d['aa1'] = 'Allen'
Nested Lists and Dictionaries 29
id8 d
3/16/17
'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1'
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}
id8 dict
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
- Can add new keys
- d['aa1'] = 'Allen'
Nested Lists and Dictionaries 30
'aa1' 'Allen' id8 d
3/16/17
'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1'
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik','aa1':'Allen'}
id8 dict
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
- Can add new keys
- d['aa1'] = 'Allen'
- Can delete keys
- del d['ela63']
Nested Lists and Dictionaries 31
'aa1' 'Allen' id8 d
3/16/17
'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1'
d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik','aa1':'Allen'}
id8 dict
Using Dictionaries (Type dict)
- Dictionaries are mutable
- Can reassign values
- d['ec1'] = 'Ellis'
- Can add new keys
- d['aa1'] = 'Allen'
- Can delete keys
- del d['ela63']
Nested Lists and Dictionaries 32
'aa1' 'Allen' id8 d
3/16/17
'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1'
Deleting key deletes both
d = {'ec1':'Ezra','ec2':'Ezra', 'aa1':'Allen'}
New Way to do Test Cases
test_cases = {4:3, 5:3, 6:6, -2:0, 20:7} for item in test_cases: seq = orig[:] # this creates a copy of the list orig print "\tTesting input " + str(seq) + ", " + str(item)
- utput = lab08.lesser_than(seq, item)
ct.assert_equals(test_cases[item], output) ct.assert_equals(seq, orig) # make sure argument list wasn't changed
3/16/17 Nested Lists and Dictionaries 33