Lecture 16 - Cornell University

[Pages:46]Lecture 16

Nested Lists and Dictionaries

Announcements for This Lecture

Prelim and Regrades

Assignments/Reading

? Regrades are now open

? Only for MAJOR mistakes ? You might lose points

? The regrade process

? Ask in Gradescope ? Tell us what to look for ? If valid, we will respond ? We will also update CMS

? Should be working on A4

? Tasks 1-2 by tomorrow ? Task 3 by the weekend ? Recursion next week

? Reading: Chapters 15, 16

? Chapter 17 for next week ? Lot of potential reading ? ... but we are covering a lot

10/22/19

Nested Lists and Dictionaries

2

Lists of Objects

? List positions are variables

? Can store base types

r id10

id13 list

? But cannot store folders ? Can store folder identifiers

b id11 g id12

x[0] id10 x[1] id11

? Folders linking to folders

x id13

x[2] id12

? Top folder for the list ? Other folders for contents

? Example:

>>> r = introcs.RGB(255,0,0) >>> b = introcs.RGB(0,0,255) >>> g = introcs.RGB(0,255,0) >>> x = [r,b,g]

id10 RGB

red 255 green 0 blue 0

id11 RGB

red 0 green 0 blue 255

id12 RGB

red 0 green 255 blue 0

10/22/19

Nested Lists and Dictionaries

3

Lists of Objects

? List positions are variables

? Can store base types

r id10

id13 list

? But cannot store folders ? Can store folder identifiers

b id11 g id12

x[0] id10 x[1] id11

? Folders linking to folders

x id13

x[2] id12

? Top folder for the list ? Other folders for contents

? Example:

>>> r = introcs.RGB(255,0,0) >>> b = introcs.RGB(0,0,255) >>> g = introcs.RGB(0,255,0) >>> x = [r,b,g]

id10 RGB

red 255 green 0 blue 0

id11 RGB

red 0 green 0 blue 255

id12 RGB

red 0 green 255 blue 0

10/22/19

Nested Lists and Dictionaries

4

Nested Lists

? Lists can hold any objects ? Lists are objects ? Therefore lists can hold other lists!

a = [2, 1] b = [3, 1] c = [1, 4, b] x = [1, a, c, 5]

x[1]

x[2] x[2][2]

x = [1, [2, 1], [1, 4, [3, 1]], 5]

x[0]

x[1][1] x[2][0]

x[2][2][1]

10/22/19

Nested Lists and Dictionaries

5

How Multidimensional Lists are Stored

? b = [[9, 6, 4], [5, 7, 7]]

9 6 4

id1

id2

id3

5 7 7

id2

9

5

id3

6

7

b id1

4

7

? b holds name of a two-dimensional list

? Has len(b) elements ? Its elements are (the names of) 1D lists

? b[i] holds the name of a one-dimensional list (of ints)

? Has len(b[i]) elements

10/22/19

Nested Lists and Dictionaries

6

Ragged Lists vs Tables

? Ragged is 2d uneven list: b = [[17,13,19],[28,95]]

b id1

id1

0

id2

1

id3

id2

0 17 1 13 2 19

id3

0 28 1 95

? Table is 2d uniform list: b = [[9,6,4],[5,7,7]]

9 6 4 5 7 7

b id1

10/22/19

id1

id2

id2

9

id3

6

4

Nested Lists and Dictionaries

id3

5 7 7

7

Nested Lists can Represent Tables

Spreadsheet

0 1 2 3 0 5473 1 4897 2 5123 3 4129 4 6780

Image

0 1 2 3 4 5 6 7 8 9 10 1112

0 1 2 3 4 5 6 7 8 9 10 11 12

table.csv

smile.xlsx

10/22/19

Nested Lists and Dictionaries

8

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

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

Google Online Preview   Download