Generators & List Comprehensions Iterable Functions C-START - - PowerPoint PPT Presentation

generators list comprehensions
SMART_READER_LITE
LIVE PREVIEW

Generators & List Comprehensions Iterable Functions C-START - - PowerPoint PPT Presentation

Generators & List Comprehensions Iterable Functions C-START Python PD Workshop C-START Python PD Workshop Generators & List Comprehensions Generator Functions Python provides a special kind of function which yield s rather than return


slide-1
SLIDE 1

Generators & List Comprehensions

Iterable Functions C-START Python PD Workshop

C-START Python PD Workshop Generators & List Comprehensions

slide-2
SLIDE 2

Generator Functions

Python provides a special kind of function which yields rather than

  • returns. This generator function is efgectively an effjcient iterable.

Consider the range function we have been using1: def range(start, stop, step=1): i = 0 while i < stop: yield i i += step Generator functions are a certain kind of the more generic generator.

1This is actually a simplifjcation C-START Python PD Workshop Generators & List Comprehensions

slide-3
SLIDE 3

Generator Functions

Python provides a special kind of function which yields rather than

  • returns. This generator function is efgectively an effjcient iterable.

Consider the range function we have been using1: def range(start, stop, step=1): i = 0 while i < stop: yield i i += step Generator functions are a certain kind of the more generic generator.

1This is actually a simplifjcation C-START Python PD Workshop Generators & List Comprehensions

slide-4
SLIDE 4

Generator Expressions

Generators can be written inline, these are called generator expressions. (x + 4 for x in nums if x % 2 == 0) There’s two parts to a generator expression:

1 Performing something for every element with for...in. 2 Selecting a subset of elements to operate on with if. This

part is optional.

C-START Python PD Workshop Generators & List Comprehensions

slide-5
SLIDE 5

Generator Expressions

Generators can be written inline, these are called generator expressions. (x + 4 for x in nums if x % 2 == 0) There’s two parts to a generator expression:

1 Performing something for every element with for...in. 2 Selecting a subset of elements to operate on with if. This

part is optional.

C-START Python PD Workshop Generators & List Comprehensions

slide-6
SLIDE 6

Generator Expressions

Generators can be written inline, these are called generator expressions. (x + 4 for x in nums if x % 2 == 0) There’s two parts to a generator expression:

1 Performing something for every element with for...in. 2 Selecting a subset of elements to operate on with if. This

part is optional.

C-START Python PD Workshop Generators & List Comprehensions

slide-7
SLIDE 7

Expression Syntax

(expression for expr in sequence1 if condition1 for expr2 in sequence2 if condition2 for expr3 in sequence3 ... if condition3 for exprN in sequenceN if conditionN) Notice the loops are evaluated outside-in.

C-START Python PD Workshop Generators & List Comprehensions

slide-8
SLIDE 8

Applications of Generator Expressions

Summing ASCII values of a string

sum(ord(c) for c in s) Note that the double-parentheses can be omitted.

File readers

reader = (float(line) for line in f) while processing_queue: process(next(reader))

Hash Function pRNGs

rng = (hashfunc(x)/MAXHASH for x in count()) diceroll(next(rng))

The possibilities are endless!

C-START Python PD Workshop Generators & List Comprehensions

slide-9
SLIDE 9

Applications of Generator Expressions

Summing ASCII values of a string

sum(ord(c) for c in s) Note that the double-parentheses can be omitted.

File readers

reader = (float(line) for line in f) while processing_queue: process(next(reader))

Hash Function pRNGs

rng = (hashfunc(x)/MAXHASH for x in count()) diceroll(next(rng))

The possibilities are endless!

C-START Python PD Workshop Generators & List Comprehensions

slide-10
SLIDE 10

Applications of Generator Expressions

Summing ASCII values of a string

sum(ord(c) for c in s) Note that the double-parentheses can be omitted.

File readers

reader = (float(line) for line in f) while processing_queue: process(next(reader))

Hash Function pRNGs

rng = (hashfunc(x)/MAXHASH for x in count()) diceroll(next(rng))

The possibilities are endless!

C-START Python PD Workshop Generators & List Comprehensions

slide-11
SLIDE 11

Applications of Generator Expressions

Summing ASCII values of a string

sum(ord(c) for c in s) Note that the double-parentheses can be omitted.

File readers

reader = (float(line) for line in f) while processing_queue: process(next(reader))

Hash Function pRNGs

rng = (hashfunc(x)/MAXHASH for x in count()) diceroll(next(rng))

The possibilities are endless!

C-START Python PD Workshop Generators & List Comprehensions

slide-12
SLIDE 12

List Comprehensions

Building lists in a syntax like generator expressions can be done simply by using square brackets. my_list = [x + 4 for x in nums if x % 2 == 0] Non-comprehensive Alternative A novice Pythonist might choose this instead: my_list = [] for x in nums: if x % 2 == 0: my_list.append(x) Why use a comprehension? It’s easier to read and faster.

C-START Python PD Workshop Generators & List Comprehensions

slide-13
SLIDE 13

List Comprehensions

Building lists in a syntax like generator expressions can be done simply by using square brackets. my_list = [x + 4 for x in nums if x % 2 == 0] Non-comprehensive Alternative A novice Pythonist might choose this instead: my_list = [] for x in nums: if x % 2 == 0: my_list.append(x) Why use a comprehension? It’s easier to read and faster.

C-START Python PD Workshop Generators & List Comprehensions

slide-14
SLIDE 14

Generic Comprehensions

The same comprehension syntax can be applied to other data structures like so: # Sets myset = {foo(x, y) for x, y in points} # Dictionaries mydict = {point: dist(p) for p in points} # Tuples mytup = tuple(foo(x, y) for x, y in points)

C-START Python PD Workshop Generators & List Comprehensions