Python iterator and itertools module, pythonitertools
Iterator
In python, The iterator protocol implements the _ iter () and next () Methods of objects. The former returns the object itself, and the latter returns the next element of the container. The objects that implement these two methods are iteratable objects. The iterator is inert and can only be generated during use. This provides the benefit of processing a large amount of data. Different iterators write all data into the memory at one time. Next, I wrote an iterator. We can see that the for loop can be used to process the iterator compiled by myself and implement the object of the iterator protocol, you can use any iterator tool similar to for loop. However, let's look at the output below. The second output is empty. Why? When using list, we can output the same object multiple times. What is the difference between this and the object implementing the iterator protocol?
1 class it(object): 2 def __init__(self, n): 3 self.a = 0 4 self.n = n 5 6 def __iter__(self): 7 return self 8 9 def next(self):10 if self.a < self.n:11 self.a += 112 return self.a13 else:14 raise StopIteration15 16 i=it(5)17 for j in i:18 print j,19 print ''20 print '------'21 for j in i:22 print j23 # 1 2 3 4 524 # ------
After learning, we know that list and other types of iterators return an iterator object instead of returning itself. The following code is written for testing. After printing and output, we can see that TestIt-like objects can be used repeatedly. So there is another problem: Is there an object that does not implement the next () method, or is it an iterator object? This is because when it class is used, the iterator object is returned, and the iteration function is implemented using the it iterator, that is, the iterator protocol is implemented. The iterator protocol is useful in python. in python, there is an itertools module for the iterator. Next I will take a look at the itertools module to see what are the surprises!
1 class TestIt(object):2 def __init__(self, a):3 self.a = a4 5 def __iter__(self):6 return it(self.a)
Itertools
Infinite iterator
1 count (). Two parameters are accepted. The first is the start number and the second is the stride. The default value starts from 0. The usage is as follows:
1 import itertools as it2 3 c = it.count(10, 2)4 for i in c:5 if i > 20:6 break7 print i,8 # 10 12 14 16 18 20
2. cycle (): receives a parameter, which is an iterator object (list, String, etc.) and cyclically generates elements in the iterator.
1 c = it.cycle([1, 2, 3])2 i = 13 for j in c:4 if i > 7:5 break6 print j,7 i += 1
3 repeat (). Two parameters are accepted to generate the first parameter n times.
1 for j in it.repeat([1, 2, 3], 4):2 print j
Limited iterator, choose to feel your common Introduction
1 chain (), accepting multiple iterator objects as parameters, and connecting them to chain ('abc', [1, 2, 3])
2 compress (data, selectors): filter the preceding parameters based on the following parameters. Both parameters must be the iterator object.
3 dropwhile (pre, iterable), The pre parameter is a function. When pre (I) is true, the return item and all the following items
4 groupby (iterable [, keyfunc]), where iterable is an iterative object, keyfunc is a grouping function, used to group the continuous items of iterable. If not specified, the same consecutive items in iterable are grouped by default, and(key, sub-iterator).
5 ifilter (function or None, sequence), The iterable function (item) is True elements constitute an iterator to return, if the function is None, returns all items in iterable that are calculated as True.
6 tee (iterable [, n]),teeCreate n Independent iterators from iterable and return them as tuples. The default value of n is 2.
1 for j in it.tee('abc', 4):2 print list(j)
Combined Generator
1 permutations (iterable [, r]) is used to generate an arrangement. r is the length of elements that generate the arrangement. If not specified, it is the default length.
1 print list(it.permutations('abc'))2 print list(it.permutations('abc', 2))3 # [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]4 # [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
2 combinations (iterable, r): returns the combination of sequences. In this case, r specifies the length of the elements that generate the combination, which is a required parameter.
3 combinations_with_replacement (iterable, r). The generated combination contains its own elements.
1 print list(it.combinations_with_replacement('abc', 2))2 # [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]