generators list comprehensions
play

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


  1. Generators & List Comprehensions Iterable Functions C-START Python PD Workshop C-START Python PD Workshop Generators & List Comprehensions

  2. Generator Functions Python provides a special kind of function which yield s rather than return s. This generator function is efgectively an effjcient iterable. Consider the range function we have been using 1 : def range(start, stop, step=1): while i < stop: yield i Generator functions are a certain kind of the more generic generator . 1 This is actually a simplifjcation C-START Python PD Workshop Generators & List Comprehensions i = 0 i += step

  3. Generator Functions Python provides a special kind of function which yield s rather than return s. This generator function is efgectively an effjcient iterable. Consider the range function we have been using 1 : def range(start, stop, step=1): while i < stop: yield i Generator functions are a certain kind of the more generic generator . 1 This is actually a simplifjcation C-START Python PD Workshop Generators & List Comprehensions i = 0 i += step

  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

  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

  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

  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

  8. reader = (float(line) for line in f) rng = (hashfunc(x)/MAXHASH for x in count()) 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 while processing_queue: process(next(reader)) Hash Function pRNGs diceroll(next(rng)) The possibilities are endless! C-START Python PD Workshop Generators & List Comprehensions

  9. rng = (hashfunc(x)/MAXHASH for x in count()) 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 while processing_queue: process(next(reader)) Hash Function pRNGs diceroll(next(rng)) The possibilities are endless! C-START Python PD Workshop Generators & List Comprehensions reader = (float(line) for line in f)

  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 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 reader = (float(line) for line in f)

  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 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 reader = (float(line) for line in f)

  12. my_list = [] List Comprehensions Building lists in a syntax like generator expressions can be done simply by using square brackets. Non-comprehensive Alternative A novice Pythonist might choose this instead: 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 my_list = [x + 4 for x in nums if x % 2 == 0]

  13. List Comprehensions Building lists in a syntax like generator expressions can be done simply by using square brackets. Non-comprehensive Alternative A novice Pythonist might choose this instead: 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 my_list = [x + 4 for x in nums if x % 2 == 0] my_list = []

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend