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

61a lecture 30 announcements data processing data
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

61A Lecture 30

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Data Processing

slide-4
SLIDE 4

Data Processing

4

slide-5
SLIDE 5

Data Processing

Many data sets can be processed sequentially:

4

slide-6
SLIDE 6

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts

4

slide-7
SLIDE 7

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election

4

slide-8
SLIDE 8

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane

4

slide-9
SLIDE 9

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

4

slide-10
SLIDE 10

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

4

slide-11
SLIDE 11

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length

4

slide-12
SLIDE 12

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length
  • A sequence allows element selection for any element

4

slide-13
SLIDE 13

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length
  • A sequence allows element selection for any element

Some important ideas in big data processing:

4

slide-14
SLIDE 14

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length
  • A sequence allows element selection for any element

Some important ideas in big data processing:

  • Implicit representations of streams of sequential data

4

slide-15
SLIDE 15

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length
  • A sequence allows element selection for any element

Some important ideas in big data processing:

  • Implicit representations of streams of sequential data
  • Declarative programming languages to manipulate and transform data

4

slide-16
SLIDE 16

Data Processing

Many data sets can be processed sequentially:

  • The set of all Twitter posts
  • Votes cast in an election
  • Sensor readings of an airplane
  • The positive integers: 1, 2, 3, ...

However, the sequence interface we used before does not always apply

  • A sequence has a finite, known length
  • A sequence allows element selection for any element

Some important ideas in big data processing:

  • Implicit representations of streams of sequential data
  • Declarative programming languages to manipulate and transform data
  • Distributed computing

4

slide-17
SLIDE 17

Iterators

slide-18
SLIDE 18

Iterators

6

slide-19
SLIDE 19

Iterators

6

A container can provide an iterator that provides access to its elements in some order

slide-20
SLIDE 20

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator

slide-21
SLIDE 21

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5]

slide-22
SLIDE 22

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s)

slide-23
SLIDE 23

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s)

slide-24
SLIDE 24

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3

slide-25
SLIDE 25

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3

slide-26
SLIDE 26

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4

slide-27
SLIDE 27

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s)

slide-28
SLIDE 28

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3

slide-29
SLIDE 29

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5

slide-30
SLIDE 30

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4

slide-31
SLIDE 31

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4

slide-32
SLIDE 32

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3}

slide-33
SLIDE 33

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d)

slide-34
SLIDE 34

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one'

slide-35
SLIDE 35

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three'

slide-36
SLIDE 36

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not >>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two'

slide-37
SLIDE 37

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two'

slide-38
SLIDE 38

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' >>> v = iter(d.values())

slide-39
SLIDE 39

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' >>> v = iter(d.values()) >>> next(v) 1

slide-40
SLIDE 40

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' >>> v = iter(d.values()) >>> next(v) 1 >>> next(v) 3

slide-41
SLIDE 41

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' >>> v = iter(d.values()) >>> next(v) 1 >>> next(v) 3 >>> next(v) 2

slide-42
SLIDE 42

Iterators

6

A container can provide an iterator that provides access to its elements in some order iter(iterable):
 next(iterator): Return an iterator over the elements 


  • f an iterable value

Return the next element in an iterator Iterators are always ordered, even if the container that produced them is not Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and

  • deletions. If keys, values and items views are

iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

>>> s = [3, 4, 5] >>> t = iter(s) >>> next(t) 3 >>> next(t) 4 >>> u = iter(s) >>> next(u) 3 >>> next(t) 5 >>> next(u) 4 >>> d = {'one': 1, 'two': 2, 'three': 3} >>> k = iter(d) >>> next(k) 'one' >>> next(k) 'three' >>> next(k) 'two' >>> v = iter(d.values()) >>> next(v) 1 >>> next(v) 3 >>> next(v) 2 (Demo)

slide-43
SLIDE 43

For Statements

slide-44
SLIDE 44

The For Statement

8

slide-45
SLIDE 45

The For Statement

for <name> in <expression>: <suite>

8

slide-46
SLIDE 46

The For Statement

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must evaluate to an iterable object

8

slide-47
SLIDE 47

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

slide-48
SLIDE 48

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

slide-49
SLIDE 49

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

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

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 >>> counts = [1, 2, 3] >>> items = iter(counts) >>> try: while True: item = next(items) print(item) except StopIteration: pass # Do nothing 1 2 3

8

slide-53
SLIDE 53

Processing Iterators

9

slide-54
SLIDE 54

Processing Iterators

9

A StopIteration exception is raised whenever next is called on an empty iterator

slide-55
SLIDE 55

Processing Iterators

9

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

slide-56
SLIDE 56

Processing Iterators

9

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

slide-57
SLIDE 57

Processing Iterators

9

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

slide-58
SLIDE 58

Processing Iterators

9

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 def contains(a, b):

slide-59
SLIDE 59

Processing Iterators

9

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 def contains(a, b): ai = iter(a)

slide-60
SLIDE 60

Processing Iterators

9

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 def contains(a, b): ai = iter(a)

slide-61
SLIDE 61

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b:

slide-62
SLIDE 62

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b:

slide-63
SLIDE 63

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-64
SLIDE 64

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-65
SLIDE 65

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-66
SLIDE 66

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-67
SLIDE 67

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-68
SLIDE 68

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-69
SLIDE 69

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-70
SLIDE 70

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-71
SLIDE 71

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing

slide-72
SLIDE 72

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing return True

slide-73
SLIDE 73

Processing Iterators

9

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 def contains(a, b): ai = iter(a) for x in b: try: while next(ai) != x: pass # do nothing except StopIteration: return False return True def contains(a, b): ai = iter(a) for x in b: while next(ai) != x: pass # do nothing return True

slide-74
SLIDE 74

Built-In Iterator Functions

slide-75
SLIDE 75

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

slide-76
SLIDE 76

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): Iterate over func(x) for x in iterable

slide-77
SLIDE 77

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x)

slide-78
SLIDE 78

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs

slide-79
SLIDE 79

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs Iterate over x in a sequence in reverse order

slide-80
SLIDE 80

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs Iterate over x in a sequence in reverse order To view the contents of an iterator, place the resulting elements into a container

slide-81
SLIDE 81

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs 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

slide-82
SLIDE 82

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs 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): tuple(iterable): Create a list containing all x in iterable Create a tuple containing all x in iterable

slide-83
SLIDE 83

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs 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): tuple(iterable): sorted(iterable): Create a list containing all x in iterable Create a tuple containing all x in iterable Create a sorted list containing x in iterable

slide-84
SLIDE 84

Built-in Functions for Iteration

Many built-in Python sequence operations return iterators that compute results lazily

11

map(func, iterable): filter(func, iterable): zip(first_iter, second_iter): reversed(sequence): Iterate over func(x) for x in iterable Iterate over x in iterable if func(x) Iterate over co-indexed (x, y) pairs 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): tuple(iterable): sorted(iterable): Create a list containing all x in iterable Create a tuple containing all x in iterable Create a sorted list containing x in iterable (Demo)

slide-85
SLIDE 85

Generators

slide-86
SLIDE 86

Generators and Generator Functions

13

slide-87
SLIDE 87

Generators and Generator Functions

13

>>> def plus_minus(x): ... yield x ... yield -x

slide-88
SLIDE 88

Generators and Generator Functions

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3)

slide-89
SLIDE 89

Generators and Generator Functions

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3

slide-90
SLIDE 90

Generators and Generator Functions

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3
slide-91
SLIDE 91

Generators and Generator Functions

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-92
SLIDE 92

Generators and Generator Functions

A generator function is a function that yields values instead of returning them

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-93
SLIDE 93

Generators and Generator Functions

A generator function is a function that yields values instead of returning them A normal function returns once; a generator function can yield multiple times

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-94
SLIDE 94

Generators and Generator Functions

A generator function is a function that yields values instead of returning them A normal function returns once; a generator function can yield multiple times A generator is an iterator created automatically by calling a generator function

13

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-95
SLIDE 95

Generators and Generator Functions

A generator function is a function that yields values instead of returning them A normal function returns 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

>>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-96
SLIDE 96

Generators and Generator Functions

A generator function is a function that yields values instead of returning them A normal function returns 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

(Demo) >>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t)

  • 3

>>> t <generator object plus_minus ...>

slide-97
SLIDE 97

Iterable User-Defined Classes

14

slide-98
SLIDE 98

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

slide-99
SLIDE 99

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

>>> list(Countdown(5)) [5, 4, 3, 2, 1]

slide-100
SLIDE 100

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

>>> list(Countdown(5)) [5, 4, 3, 2, 1] >>> for x in Countdown(3): ... print(x) 3 2 1

slide-101
SLIDE 101

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

class Countdown: def __init__(self, start): self.start = start >>> list(Countdown(5)) [5, 4, 3, 2, 1] >>> for x in Countdown(3): ... print(x) 3 2 1

slide-102
SLIDE 102

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

class Countdown: def __init__(self, start): self.start = start def __iter__(self): >>> list(Countdown(5)) [5, 4, 3, 2, 1] >>> for x in Countdown(3): ... print(x) 3 2 1

slide-103
SLIDE 103

Iterable User-Defined Classes

The special method __iter__ is called by the built-in iter() & should return an iterator

14

class Countdown: def __init__(self, start): self.start = start def __iter__(self): v = self.start while v > 0: yield v v -= 1 >>> list(Countdown(5)) [5, 4, 3, 2, 1] >>> for x in Countdown(3): ... print(x) 3 2 1

slide-104
SLIDE 104

Generators & Iterators

slide-105
SLIDE 105

Generators can Yield from Iterators

16

slide-106
SLIDE 106

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

slide-107
SLIDE 107

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

>>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6]

slide-108
SLIDE 108

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

def a_then_b(a, b): for x in a: yield x for x in b: yield x >>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6]

slide-109
SLIDE 109

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

def a_then_b(a, b): yield from a yield from b def a_then_b(a, b): for x in a: yield x for x in b: yield x >>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6]

slide-110
SLIDE 110

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

def a_then_b(a, b): yield from a yield from b def a_then_b(a, b): for x in a: yield x for x in b: yield x >>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6] >>> list(countdown(5)) [5, 4, 3, 2, 1]

slide-111
SLIDE 111

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

def a_then_b(a, b): yield from a yield from b def a_then_b(a, b): for x in a: yield x for x in b: yield x def countdown(k): if k > 0: yield k yield from countdown(k-1) >>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6] >>> list(countdown(5)) [5, 4, 3, 2, 1]

slide-112
SLIDE 112

Generators can Yield from Iterators

A yield from statement yields all values from an iterator or iterable (Python 3.3)

16

def a_then_b(a, b): yield from a yield from b def a_then_b(a, b): for x in a: yield x for x in b: yield x def countdown(k): if k > 0: yield k yield from countdown(k-1) >>> list(a_then_b([3, 4], [5, 6])) [3, 4, 5, 6] >>> list(countdown(5)) [5, 4, 3, 2, 1] (Demo)