61a lecture 30 announcements data processing data
play

61A Lecture 30 Announcements Data Processing Data Processing 4 - PowerPoint PPT Presentation

61A Lecture 30 Announcements Data Processing Data Processing 4 Data Processing Many data sets can be processed sequentially: 4 Data Processing Many data sets can be processed sequentially: The set of all Twitter posts 4 Data


  1. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' 6

  2. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' 6

  3. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' 6

  4. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) arbitrary order which is non-random, varies >>> next(k) across Python implementations, and depends on 'one' the dictionary’s history of insertions and >>> next(k) deletions. If keys, values and items views are 'three' iterated over with no intervening modifications >>> next(k) to the dictionary, the order of items will 'two' directly correspond. 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  5. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) >>> v = iter(d.values()) arbitrary order which is non-random, varies >>> next(k) across Python implementations, and depends on 'one' the dictionary’s history of insertions and >>> next(k) deletions. If keys, values and items views are 'three' iterated over with no intervening modifications >>> next(k) to the dictionary, the order of items will 'two' directly correspond. 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  6. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) >>> v = iter(d.values()) arbitrary order which is non-random, varies >>> next(k) >>> next(v) across Python implementations, and depends on 'one' 1 the dictionary’s history of insertions and >>> next(k) deletions. If keys, values and items views are 'three' iterated over with no intervening modifications >>> next(k) to the dictionary, the order of items will 'two' directly correspond. 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  7. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) >>> v = iter(d.values()) arbitrary order which is non-random, varies >>> next(k) >>> next(v) across Python implementations, and depends on 'one' 1 the dictionary’s history of insertions and >>> next(k) >>> next(v) deletions. If keys, values and items views are 'three' 3 iterated over with no intervening modifications >>> next(k) to the dictionary, the order of items will 'two' directly correspond. 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  8. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) >>> v = iter(d.values()) arbitrary order which is non-random, varies >>> next(k) >>> next(v) across Python implementations, and depends on 'one' 1 the dictionary’s history of insertions and >>> next(k) >>> next(v) deletions. If keys, values and items views are 'three' 3 iterated over with no intervening modifications >>> next(k) >>> next(v) to the dictionary, the order of items will 'two' 2 directly correspond. 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  9. Iterators A container can provide an iterator that provides access to its elements in some order iter(iterable): 
 Return an iterator over the elements 
 >>> s = [3, 4, 5] >>> u = iter(s) of an iterable value >>> t = iter(s) >>> next(u) >>> next(t) 3 next(iterator): Return the next element in an iterator 3 >>> next(t) >>> next(t) 5 4 >>> next(u) 4 Iterators are always ordered, even if the container that produced them is not >>> d = {'one': 1, 'two': 2, 'three': 3} Keys and values are iterated over in an >>> k = iter(d) >>> v = iter(d.values()) arbitrary order which is non-random, varies >>> next(k) >>> next(v) across Python implementations, and depends on 'one' 1 the dictionary’s history of insertions and >>> next(k) >>> next(v) deletions. If keys, values and items views are 'three' 3 iterated over with no intervening modifications >>> next(k) >>> next(v) to the dictionary, the order of items will 'two' 2 directly correspond. (Demo) 6 https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

  10. For Statements

  11. The For Statement 8

  12. The For Statement for <name> in <expression>: <suite> 8

  13. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 8

  14. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: 8

  15. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: A.Bind <name> to that element in the first frame of the current environment 8

  16. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: A.Bind <name> to that element in the first frame of the current environment B.Execute the <suite> 8

  17. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: A.Bind <name> to that element in the first frame of the current environment B.Execute the <suite> When executing a for statement, iter returns an iterator and next provides each item: 8

  18. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: A.Bind <name> to that element in the first frame of the current environment B.Execute the <suite> When executing a for statement, iter returns an iterator and next provides each item: >>> counts = [1, 2, 3] >>> for item in counts: print(item) 1 2 3 8

  19. The For Statement for <name> in <expression>: <suite> 1. Evaluate the header <expression>, which must evaluate to an iterable object 2. For each element in that sequence, in order: A.Bind <name> to that element in the first frame of the current environment B.Execute the <suite> When executing a for statement, iter returns an iterator and next provides each item: >>> counts = [1, 2, 3] >>> counts = [1, 2, 3] >>> items = iter(counts) >>> for item in counts: >>> try: print(item) while True: 1 item = next(items) 2 print(item) 3 except StopIteration: pass # Do nothing 1 2 3 8

  20. Processing Iterators 9

  21. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator 9

  22. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') True 9

  23. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') True >>> contains('strength', 'rest') False 9

  24. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') True >>> contains('strength', 'rest') False >>> contains('strength', 'tenth') True 9

  25. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True >>> contains('strength', 'rest') False >>> contains('strength', 'tenth') True 9

  26. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') False >>> contains('strength', 'tenth') True 9

  27. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') False >>> contains('strength', 'tenth') True 9

  28. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') True 9

  29. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') True 9

  30. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  31. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  32. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  33. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  34. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  35. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  36. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  37. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  38. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing 9

  39. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): True ai = iter(a) >>> contains('strength', 'rest') for x in b: False >>> contains('strength', 'tenth') while next(ai) != x: True pass # do nothing return True 9

  40. Processing Iterators A StopIteration exception is raised whenever next is called on an empty iterator >>> contains('strength', 'stent') def contains(a, b): def contains(a, b): True ai = iter(a) ai = iter(a) >>> contains('strength', 'rest') for x in b: for x in b: False try: >>> contains('strength', 'tenth') while next(ai) != x: while next(ai) != x: True pass # do nothing pass # do nothing except StopIteration: return False return True return True 9

  41. Built-In Iterator Functions

  42. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily 11

  43. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable 11

  44. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): 11

  45. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs 11

  46. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order 11

  47. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container 11

  48. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container list(iterable): Create a list containing all x in iterable 11

  49. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container list(iterable): Create a list containing all x in iterable tuple(iterable): Create a tuple containing all x in iterable 11

  50. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container list(iterable): Create a list containing all x in iterable tuple(iterable): Create a tuple containing all x in iterable sorted(iterable): Create a sorted list containing x in iterable 11

  51. Built-in Functions for Iteration Many built-in Python sequence operations return iterators that compute results lazily map(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) filter(func, iterable): zip(first_iter, second_iter): Iterate over co-indexed (x, y) pairs reversed(sequence): Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container list(iterable): Create a list containing all x in iterable tuple(iterable): Create a tuple containing all x in iterable sorted(iterable): Create a sorted list containing x in iterable (Demo) 11

  52. Generators

  53. Generators and Generator Functions 13

  54. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x 13

  55. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) 13

  56. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 13

  57. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 13

  58. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> 13

  59. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> A generator function is a function that yield s values instead of return ing them 13

  60. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> A generator function is a function that yield s values instead of return ing them A normal function return s once; a generator function can yield multiple times 13

  61. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> A generator function is a function that yield s values instead of return ing them A normal function return s once; a generator function can yield multiple times A generator is an iterator created automatically by calling a generator function 13

  62. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> A generator function is a function that yield s values instead of return ing them A normal function return s once; a generator function can yield multiple times A generator is an iterator created automatically by calling a generator function When a generator function is called, it returns a generator that iterates over its yields 13

  63. Generators and Generator Functions >>> def plus_minus (x): ... yield x ... yield -x >>> t = plus_minus( 3 ) >>> next(t) 3 >>> next(t) -3 >>> t <generator object plus_minus ...> A generator function is a function that yield s values instead of return ing them A normal function return s once; a generator function can yield multiple times A generator is an iterator created automatically by calling a generator function When a generator function is called, it returns a generator that iterates over its yields (Demo) 13

  64. Iterable User-Defined Classes 14

  65. Iterable User-Defined Classes The special method __iter__ is called by the built-in iter() & should return an iterator 14

  66. Iterable User-Defined Classes The special method __iter__ is called by the built-in iter() & should return an iterator >>> list(Countdown(5)) [5, 4, 3, 2, 1] 14

  67. Iterable User-Defined Classes The special method __iter__ is called by the built-in iter() & should return an iterator >>> list(Countdown(5)) [5, 4, 3, 2, 1] >>> for x in Countdown(3): ... print(x) 3 2 1 14

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