I. INTRODUCTION
itertools iterator is python module, the tool provides relatively efficient and itertools save memory. Using these tools, you will be able to create your own custom loop iteration for high efficiency.
Second, a code module
1, combinations of elements
itertools.combinations(iterable, r)
- iterable: an iteration object.
- r: r take tierable object elements are combined.
Note: When the function returns an iterator.
Code Example:
Import the itertools X = [l, 2,3 ] combin_1 = The itertools.combinations (X, 1) # take a number combination, generated when an iterator combin_2 The itertools.combinations = (X, 2) # take the number 2 combining a generated iterator Print ( ' combin_1: ' , list (combin_1)) # iterator into a list and the print Print ( ' \ ncombin_1, combin_2 data types: ' , type (combin_1)) Print ( ' \ n-output iterator each iteration content: ' ) for I in combin_2: Print (I) '' ' results: combin_1: [(1), (2,), (3,)] combin_1, combin_2 data type: <class 'itertools.combinations'> output iterator each iteration contents: (1, 2) (1, 3 ) (2, 3) '' '
2、数据的排列
itertools.combinations(iterable, r)
- iterable: an iteration object.
- r: r take tierable object elements are combined.
Note: When the function returns an iterator.
Import the itertools X = [l, 2,3 ] combin_1 = the itertools.permutations (X, 1) # take a number combination, generated when an iterator combin_2 the itertools.permutations = (X, 2) # take the number 2 combining a generated iterator Print ( ' combin_1: ' , list (combin_1)) # iterator into a list and the print Print ( ' \ ncombin_1, combin_2 data types: ' , type (combin_1)) Print ( ' \ n-output iterator each iteration content: ' ) for I in combin_2: Print (I) '' ' results: combin_1: [(1), (2,), (3,)] combin_1, combin_2 data type: <class 'itertools.permutations'> output iterator each iteration contents: (1, 2) (1, 3 ) (2, 1) (2, 3) (3, 1) (3, 2) '' '