EECS 110: Lec 5: List Comprehensions - PowerPoint PPT Presentation

About This Presentation
Title:

EECS 110: Lec 5: List Comprehensions

Description:

Homework 2 - this coming Sunday...! 3 problems. 1 lab problem Tuesday. 2 python problems ... def range(low,hi): ''' input: two ints, low and hi. output: int ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 31
Provided by: csNorth
Category:

less

Transcript and Presenter's Notes

Title: EECS 110: Lec 5: List Comprehensions


1
EECS 110 Lec 5 List Comprehensions
  • Aleksandar Kuzmanovic
  • Northwestern University

http//cs.northwestern.edu/akuzma/classes/EECS110
-s09/
2
EECS 110 today
data, sequences
The building blocks of functional computing
conditionals
recursion
List Comprehensions
map and applications
Homework 2 - this coming Sunday!
1 lab problem Tuesday
3 problems
Homework 1 - submitted
2 python problems
3
functional programming
gtgtgt 'fun' in 'functional' True
  • representation via list structures (data)
  • leverage self-similarity (recursion)
  • create small building blocks (functions)

Compose these together to solve or investigate
problems.
Key ideas in functional programming
not maximally efficient for the computer
elegant and concise
vs.
4
return to recursion
Composing functions into specific applications
Creating general functions that will be useful
everywhere (or almost)
5
return to recursion
Composing functions into specific applications
Creating general functions that will be useful
everywhere (or almost)
building blocks with which to compose
6
sum, range
def sum(L) """ input a list of numbers, L
output L's sum """
7
sum, range
def sum(L) """ input a list of numbers, L
output L's sum """ if len(L)
0 return 0.0 else return
L0 sum(L1)
Base Case
This input to the recursive call must be
"smaller" somehow
if the input has no elements, its sum is zero
Recursive Case
if L does have an element, add that element's
value to the sum of the REST of the list
8
sum, range
def range(low,hi) """ input two ints, low
and hi output int list from low up to
hi """
excluding hi
9
sum, range
def range(low,hi) """ input two ints, low
and hi output int list from low up to
hi """ if hi lt low return
else return
excluding hi
10
sum, range
def range(low,hi) """ input two ints, low
and hi output int list from low up to
hi """ if hi lt low return
else return low range(low1,hi)
excluding hi
11
sum and range
gtgtgt sum(range(101))
Looks sort of scruffy for a 7-year old !
and 100 more
12
Recursion Good News/Bad News
Recursion is common (fundamental) in functional
programming
def dblList(L) """ Doubles all the values in
a list. input L, a list of numbers """
if L return L else
return L02 dblList(L1)
But you can sometimes hide it away!
13
Map The recursion "alternative"
def dbl(x) return 2x
gtgtgt map( dbl, 0,1,2,3,4,5 ) 0, 2, 4, 6, 8, 10
def sq(x) return x2
gtgtgt map( sq, range(6) ) 0, 1, 4, 9, 16, 25
(1) map always returns a list
(2) map(f,L) calls f on each item in L
def isana(x) return x'a
gtgtgt map( isana, 'go away!' ) 0, 0, 0, 1, 0, 1,
0, 0
Hey this looks a bit False to me!
14
Map !
def dblList(L) """ Doubles all the values in
a list. input L, a list of numbers """
if L return L else
return L02 dblList(L1)
Without map
def dbl(x) return x2 def dblList(L)
""" Doubles all the values in a list.
input L, a list of numbers """ return
map(dbl, L)
With map!
15
Map a higher-order function
In Python, functions can take other functions as
input
def map( f, L )
KeyConcept
Functions ARE data!
16
Why use map?
17
Why use map?
More elegant / shorter code, functional in style
Faster execution in Python map optimized for
operations in lists
Avoid rewriting list recursion (build once, use
lots)
18
Mapping without map List Comprehensions
Anything you want to happen to each element of a
list
name that takes on the value of each element in
turn
the list (or string)
any name is OK!
gtgtgt dbl(x) for x in 0,1,2,3,4,5 0, 2, 4,
6, 8, 10
input
output
input
gtgtgt x2 for x in range(6) 0, 1, 4, 9, 16,
25
output
gtgtgt c 'a' for c in 'go away!' 0, 0, 0, 1,
0, 1, 0, 0
input
output
19
Mapping without map List Comprehensions
def dbl(x) return 2x
gtgtgt map( dbl, 0,1,2,3,4,5 ) 0, 2, 4, 6, 8, 10
gtgtgt dbl(x) for x in 0,1,2,3,4,5 0, 2, 4,
6, 8, 10
def sq(x) return x2
gtgtgt map( sq, range(6) ) 0, 1, 4, 9, 16, 25
gtgtgt x2 for x in range(6) 0, 1, 4, 9, 16,
25
gtgtgt map( isana, 'go away!' ) 0, 0, 0, 1, 0, 1,
0, 0
def isana(x) return x'a
gtgtgt c 'a' for c in 'go away!' 0, 0, 0, 1,
0, 1, 0, 0
20
List Comprehensions
def len(L) if L return 0
else return 1 len(L1)
len(L)
def sajak(s) if len(s) 0 return
0 else if s0 not in 'aeiou'
return sajak(s1) else
return 1sajak(s1)
sajak(s)
def sScore(s) if len(s) 0 return
0 else return letScore(s0)
\ sScore(s1)
sScore(s)
scrabble score
implemented via raw recursion
21
List Comprehensions
len(L)
LC 1 for x in L return sum( LC )
22
List Comprehensions
len(L)
LC 1 for x in L return sum( LC )
sajak(s)
of vowels
LC c in 'aeiou' for c in s return sum( LC )
23
List Comprehensions
len(L)
LC 1 for x in L return sum( LC )
sajak(s)
of vowels
LC c in 'aeiou' for c in s return sum( LC )
sScore(s)
scrabble score
LC letScore(c) for c in s return sum( LC )
24
Quiz
Write each of these functions concisely using
list comprehensions
Name(s)
Write
input e, any element L, any list or string
Remember True 1 and False 0
output the of times L contains e
def count(e,L)
example count('f', 'fluff') 3
W are the winning numbers
Write
input Y and W, two lists of lottery numbers
(ints)
Y are your numbers
def lotto(Y,W)
output the of matches between Y W
example lotto(5,7,42,44,3,5,7,44) 3
Extra! Write
input N, an int gt 2
output the number of positive divisors of N
def divs(N)
example divs(12) 6 (1,2,3,4,6,12)
25
Quiz
26
Quiz
count(e,L)
LC xe for x in L return sum( LC )
27
Quiz
lotto(Y,W)
LC c in Y for c in W return sum( LC )
28
Quiz
divs(N)
LC Nc0 for c in range(1,N1) return sum(
LC )
29
Quiz
count(e,L)
LC xe for x in L return sum( LC )
lotto(Y,W)
LC c in Y for c in W return sum( LC )
divs(N)
LC Nc0 for c in range(1,N1) return sum(
LC )
30
See you at Lab!
Write a Comment
User Comments (0)
About PowerShow.com