Saturday 1 September 2012

Python iterators, generators and list comprehension

Iterators

Iterators are objects which enables traversal of python sequence data structures like strings, tuples and lists which are so called iterable.
>>> list=[1,2,3,4]
>>> lit=iter(list)
>>> lit
<listiterator object at 0x96abd2c>
 we can use this iterator object to extract elements on the list one by one
>>> lit.next()
1
>>> lit.next()
2
>>> lit.next()
3
>>> lit.next()
4
>>> lit.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
Function next() raise Stopiteration exception when there is no more element in list to traverse.

 we can use iterator object instead of corresponding sequence variables in construct.
>>> tit=iter((1,2,3,4))
>>> tit
<tupleiterator object at 0x96abfcc>
>>> 3 in tit
True 

generators

>>> def listgen(n):
...     for i in range(n):
...             yield i
... 
Python interpret functions like above containing a yield statement in a special way unlike to other functions. When we call the function listgen(n)
>>> g=listgen(5)
>>> g
<generator object listgen at 0xb786af7c>
Instead of executing the function and returning the result, a generator object  corresponding to listgen() is returned. The use of generator object is similar to iterator object.
>>> g=listgen(3)
>>> g.next()
0
>>> g.next()
1
>>> g.next()
2
>>> g.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration 

List comprehension

squares=[i*i for i in range(10)  if i%2==1]
>>> squares
[1, 9, 25, 49, 81]
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more if clauses. This special construct returns a list. The above list comprehension statement gives same result as
>>> squares=[]
>>> for i in range(10):
...     if i%2==1:
...             squares.append(i*i)
... 
 or
>>>squares=map((lambda x: x*x),filter((lambda x:x%2==1), range(10)))

We can perform nested list comprehension like follows,
>>> matrix=[[1,2,3],[4,5,6],[7,8,9]]
>>> transpose=[[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))]
>>> transpose
[[1, 4, 7], [2, 5, 8], [3, 6, 9]] 



 

1 comment:

  1. Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete