UW CSE 140 Winter 2014 .edu

List comprehensions

UW CSE 140

Winter 2014

1

Ways to express a list

1. Explicitly write the whole thing:

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

2. Write a loop to create it:

squares = []

for i in range(11):

squares.append(i*i)

3. Write a list comprehension:

squares = [i*i for i in range(11)]

A list comprehension is a concise description of a list

A list comprehension is shorthand for a loop

2

Mathematical notation

Let I be the integers

? { x : x?? I and x = x2 } is the set { 0, 1 }

? { x : x?? I and x > 0 } is the set of all positive

integers

? { x2 : x?? I and 0 ¡Ü x < 10 and prime(x) }

expression variable domain

condition

Python notation:

? { x*x for x in range(10) if prime(x) }

expression

variable

domain

condition

3

Two ways to convert Centigrade to

Fahrenheit

ctemps = [17.1, 22.3, 18.4, 19.1]

With a loop:

ftemps = []

for c in ctemps:

f = celsius_to_farenheit(c)

ftemps.append(f)

With a list comprehension:

ftemps = [celsius_to_farenheit(c) for c in ctemps]

The comprehension is usually shorter, more readable, and more efficient

4

Syntax of a comprehension

[(x,y) for x in org1 for y in org2 if sim(x,y) > threshold]

expression

for clause (required)

assigns value to the

variable x

zero or more

additional

for clauses

zero or more if clauses

something

that can be

iterated

5

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

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

Google Online Preview   Download