Python Tutorial
for i in [1, 2, 3, 4]: print i,Output:
$ python iterators.py 1 2 3 4
or ch in "python": print ch,Output:
$ python iterators.py p y t h o n
for key, val in {"key1": "value1", "key2": "value2"}.items(): print key, "->", valOutput:
$ python iterators.py key2 -> value2 key1 -> value1
new_list = list("python"); print new_list;Output:
$ python iterators.py ['p', 'y', 't', 'h', 'o', 'n']
new_list = list("python"); iterator = iter(new_list); print iterator.next(); print iterator.next(); print iterator.next(); print iterator.next(); print iterator.next(); print iterator.next(); print iterator.next();Output:
$ python iterators.py p y t h o n Traceback (most recent call last): File "iterators.py", line 9, inonce traverse all the elements in list, throws StopIteration error if try next() method.print iterator.next(); StopIteration
class yrange: def __init__(self, n): self.i = 0 self.n = n def __iter__(self): return self def next(self): if self.i < self.n: i = self.i self.i += 1 return i else: raise StopIteration() r = yrange(4); print r.next(); print r.next(); print r.next(); print r.next(); print r.next(); print r.next();here iterator works like built-in xrange function.
$ python iterators.py 0 1 2 3 Traceback (most recent call last): File "iterators.py", line 22, inprint r.next(); File "iterators.py", line 15, in next raise StopIteration() StopIteration
def yrange(n): i = 0 while i < n: yield i i += 1 r = yrange(4); print r.next(); print r.next(); print r.next(); print r.next(); print r.next(); print r.next();Output:
$ python iterators.py 0 1 2 3 Traceback (most recent call last): File "iterators.py", line 12, inprint r.next(); StopIteration
def yrange(n): i = 0 while i < n: print "before yield"; yield i print "after yield"; i += 1 r = yrange(4); print "next: ", r.next(); print "next: ", r.next(); print "next: ", r.next(); print "next: ", r.next(); print "next: ", r.next();Output:
$ python iterators.py next: before yield 0 next: after yield before yield 1 next: after yield before yield 2 next: after yield before yield 3 next: after yield Traceback (most recent call last): File "iterators.py", line 14, inprint "next: ", r.next(); StopIteration
result = (num*2 for num in range(10)) print resultOutput:
$ python iterators.pyat 0x7f77a64e77d0>
result = (num*2 for num in range(2)) print result.next(); print result.next(); print "sum: ", sum(result);Output:
$ python iterators.py 0 2 sum: 0Correct way to use math functions:
result = (num*2 for num in range(2)) print "sum: ", sum(result);Output:
$ python iterators.py sum: 2
import itertools; iterator1 = iter([1, 2, 3, 4]) iterator2 = iter(['A', 'B', 'C']) print itertools.chain(iterator1, iterator2); print list(itertools.chain(iterator1, iterator2));Output:
$ python iterators.py[1, 2, 3, 4, 'A', 'B', 'C']
print enumerate(["one", "two", "three"]); print list(enumerate(["one", "two", "three"]));Output:
$ python iterators.py[(0, 'one'), (1, 'two'), (2, 'three')]
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page