python
A beginner guide for python..
Iterators & Generators -3
Itertools
The itertools module in the standard library provides lot of intersting tools to work with iterators.Lets look at some of the interesting functions.
chain – chains multiple iterators together.
>>> it1 = iter([1, 2, 3])
>>> it2 = iter([4, 5, 6])
>>> itertools.chain(it1, it2)
[1, 2, 3, 4, 5, 6]
>>> for x, y in itertools.izip(["a", "b", "c"], [1, 2, 3]):
... print x, y
...
a 1
b 2
c 3
>>> it = iter(range(5))
>>> x, it1 = peep(it)
>>> print x, list(it1)
0 [0, 1, 2, 3, 4]
>>> list(enumerate(["a", "b", "c"])
[(0, "a"), (1, "b"), (2, "c")]
>>> for i, c in enumerate(["a", "b", "c"]):
... print i, c
...
0 a
1 b
2 c
Labels:
Itertools,
problems,
programs,
python,
python problems,
python programs.
Subscribe to:
Posts (Atom)