Iterators in Python 2 - Drexel University
Iterators in Python 2 Kurt Schmidt
Intro Comprehensions zip Creating
Iterators in Python 2
Kurt Schmidt
Dept. of Computer Science, Drexel University
September 21, 2021
Overview
Iterators in Python 2 Kurt Schmidt
Intro Comprehensions zip Creating
? An iterator iterates over an iterable object ? Examples of iterable objects:
? str list tuple dict file ? An iterator serves up items one at a time
? Objects can serve up an iterator (often itself) ? Invoked in the for loop
1 l = [1, 2, 3]
2 for i in l :
3
print i
str doesn't have an iter method; the iter() operator sees the getitem (idx) method, builds an iterator.
Built-in Iterators
Iterators in Python 2 Kurt Schmidt
Intro Comprehensions zip Creating
? list, tuple, dict, others, provide an iterator upon asking
? next(...) accesses values, moves the iterator along ? Raises the StopIteration exception when it's done
1 l = [1, 2, 3]
2 it = iter( l )
3 try:
4
while True :
5
print( next( it ))
6
except StopIteration , e :
7
print( "\nAnd , that's all\n" )
Avoiding the StopIteration
Iterators in Python 2 Kurt Schmidt
Intro Comprehensions zip Creating
? There is no hasNext(...) predicate
? Raises the StopIteration exception when it's done ? next( iter [, default] ) will take a default value to
return, rather than raising an exception ? Choose a generic object, used for its unique reference
1 l = [1, 2, 3]
2 END_ITER = object() # just a sentinel reference
3
4 v = next( l, END_ITER )
5 while v is not END_ITER :
6
print( v )
7
v = next( l, END_ITER )
8 print( "\nAnd , that's all\n" )
for-loop calls iter(...)
Iterators in Python 2
Kurt Schmidt
Intro
Comprehensions zip
Creating 1 2 3 4 5
? for i in iterable expects iterable to be iterable ? Class iter( iterable ) ? Takes care of catching StopIteration
l = [1, 2, 3]
for i in l : print( i )
print( "\nAnd , that's all\n" )
1 2 3
And, that's all
List Comprehensions
Iterators in Python 2
Kurt Schmidt
Intro
Comprehensions zip
1 Creating
2 3 4 5 6
? Construct lists in a natural way
? Behaves as map and filter
L = [ x**2 for x in range( 1, 11 )] M = [ x for x in L if x%2==0 ] print L print M # print the even squares print [ "BUZZ" if i%7==0 else i
for i in range (15) ]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [4, 16, 36, 64, 100] [1, 2, 3, 4, 5, 6, 'BUZZ', 8, 9, 10, 11,
12, 13, 'BUZZ', 15]
Nested List Comprehensions
Iterators in Python 2
Kurt Schmidt
Intro
Comprehensions
zip
1
Creating
2
3
4
? Let's make a multiplication table:
t = [ [ x*y for x in range(1, 6) ] for y in range(1, 6) ]
for r in t : print r
[1, 2, 3, 4, 5] [2, 4, 6, 8, 10] [3, 6, 9, 12, 15] [4, 8, 12, 16, 20] [5, 10, 15, 20, 25]
Zipped Comprehensions
Iterators in Python 2
Kurt Schmidt
Intro
Comprehensions zip
Creating
1
2
3
4
5
? We can iterate over two or more lists simultaneously
first = [ 'Hedda', 'Bilbo', 'John' ] last = [ 'Gabler', 'Baggins', 'Watson' ] names = [ ' '.join( (f,l) )
for (f,l) in zip( first , last ) ] print names
['Hedda Gabler', 'Bilbo Baggins', 'John Watson']
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.