Understanding List Processing: Map, For, Nested Lists, 2D Lists, and Slices, Slides of Introduction to Computing

An overview of various list processing techniques in python, including built-in functions sum, min, max, range, list, map, and the for statement. It also covers nested lists and two-dimensional lists, as well as slices and multidimensional lists. Students will learn how to manipulate and process lists using these techniques.

Typology: Slides

2012/2013

Uploaded on 08/17/2013

bakul
bakul 🇮🇳

4.6

(16)

69 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Processing lists: builtins!
sum(x) adds up all the elements in the list x!
§they had better be numbers!!
min(x) or max(x) find the minimum resp. maximum
value in the list x!
§they use the same ordering as sort()!
range(n) produces [0, 1, 2, …, n–1]!
§optional arguments to start somewhere other than zero!
list(x) converts x (a string for example) to a list!
§e.g. list('mimsy') produces [“m”, “i”, “m”, “s”, “y”]!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding List Processing: Map, For, Nested Lists, 2D Lists, and Slices and more Slides Introduction to Computing in PDF only on Docsity!

Processing lists: builtins

• sum(x) adds up all the elements in the list x

§ they had better be numbers!

• min(x) or max(x) find the minimum resp. maximum

value in the list x

§ they use the same ordering as sort()

• range( n ) produces [0, 1, 2, …, n –1]

§ optional arguments to start somewhere other than zero

• list(x) converts x (a string for example) to a list

§ e.g. list('mimsy') produces [“m”, “i”, “m”, “s”, “y”]

map(f, x)

Processing lists: The map Function

map(m, x)

examples:

map(len, ['a', 'bc', 'defg']) produces [1, 2, 4]

map(str.strip, ['a ', ' bc', ' defg ']) produces [“a”, “bc”, “defg”]

if x is a list of n items and  f is a function with one parameter:

[f(x[0]), f(x[1]), …, f(x[ n –1])]

map(m, x)

[x[0].m(), x[1].m(), …, x[ n –1].m()]

if x is a list of n items and  m is a method with no parameters:

General form: map(⟨ function ⟩, ⟨ list ⟩)

calls the function once for each list item

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][0] x[2][2][1] x[1] x[2]

a = [2, 1] x[2][2]

b = [3, 1]

c = [1, 4, b]

x = [1, a, c, 5]

Two Dimensional Lists

Table of Data Images

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 10 11 12 0 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

How Multidimensional Lists are Stored

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

• b holds name of a one-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

id 9 6 4 id 5 7 7 id id id b^ id

Image Data: 2D Lists of Pixels

0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 b^ id id id id list … id id id list … id red 255 green 255 blue 255

RGB

b[0][0] is a

white pixel

Slices and Multidimensional Lists

• Only “top-level” list is copied.

• Contents of the list are not altered

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

id 9 6 id id id b id id id 4 5 id 7 7

x = b[:2]

x id id id id

Slices and Multidimensional Lists

• Create a 2D 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 the 2D list

[[9, 6], [4, 5, 10]]

• What are the contents of

the list (with name) in b?

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