1. What is an iteration?
First look at three examples:
When the object of the iteration is a list object, he prints every list object
For i in [1,2,3,4]: print (i) printed results: 1234
When we iterate over the object is a string, he will print out each character of the string
For C in ' Python ': print (c) The result of printing is: Python
When the object of our iteration is a dictionary (dict), he will traverse his keys
For k in {' X ': 1, ' Y ': 2}: Print (k) The result is: xyadd1: (sometimes we will find that the result of printing is: YX
Why is it?
The Dict storage is not arranged in the order of list, so the result order of the iteration is probably not the same.
ADD2: (see this there may be classmates ask, can I iterate through the value of dict?
The answer is yes, after all, what's so powerful about python, exactly? Look at me:
For value in {' X ': 1, ' Y ': 2}.values (): print (value) The result is: 1 2 or is it??
I want to see the above you should be the two question mark how to fill it. If you still do not fill in these two question marks, I would advise you to go back and look at this article the most
But, wait, wait, some classmates say, I also want to traverse the key and value together. May I?
For k,v in {' X ': 1, ' Y ': 2}.items (): Print (K,V) The result is: X 1 y 2 or: Y 2 x 1
So the question comes, will it appear, key and value do not match? It's not going to be hundreds of thousands of times after I've run.
Why is it? Think for yourself.
)
So, you can see a lot of objects that can iterate, list,string,tuple,dict ... Can be used as an iterative object
Do you know the meaning of the iterator now?
Iterators are a way to access elements within a collection, iterating over all the elements of a collection
2. Generation of iterators
Python built-in function ITER needs to iterate over an object and return an iterator
X=iter ([+]) print (Next (x)) print (Next (x)) The result is: <list_iterator object at 0x0000000001ea2908> 123File "D:\Python\xode\try.py", line 6, in <module> print (next (x)) stopiteration
Each call to next will access the next element
The stopiteration exception will be thrown when the element is traversed and if the continuation call Next,python encounters such a condition
3.Itertools
Python has a built-in module Itertools, which contains a number of functions for the creating iterators for efficient looping (creating a more efficient loop iterator)
3.1
Itertools.accumulate (iterable[, Func])
Returns the accumulated sum, which can be any type, including the addition of decimals or fractions,
If an optional function parameter is provided, it should be a function of two parameters, which will replace the addition
In short, it is:
If a list object is LIST=[P0,P1,P2,P3,P4], then through Itertools.accumulate ()
The result of the return is [P0,P0+P1,P0+P1+P2,P0+P1+P2+P3,P0+P1+P2+P3+P4]
Example: You must import the Itertools package
Import Itertoolsd=[6,21,5,3,4,8,62,54,12,0]de=iter (d) Print (list (itertools.accumulate)) The result is: [6, 27, 32, 35, 39 , 47, 109, 163, 175, 175]
3.2
Itertools.chain (*iterables)
Causes the returned element to be exhausted from the first until the exhausted iterator, and then proceeds to the next until all of the iterator objects are depleted. For continuous sequences as a single sequence
Instance:
Import itertoolspartlist1= ' ABC ' partlist2= ' DEF ' partlist3= ' GHI ' Print (List (Itertools.chain, PARTLIST3))) The result of the printing is: [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ']
Itertools.chain is to connect all the lists together.
3.3
Itertools.combinations (iterable, R)
Returns the element input iterator for the subsequence of the R-length, issued in a combined dictionary sort order,
So if the input iterators are sorted, the combination will result in a tuple sort, each element is a unique element based on their location, not according to their value
So if the input element is unique, there is no duplicate value in each combination
Example:
Import itertoolspartlist1= ' 1234 ' Print (List (itertools.combinations (partlist1,2))) Prints the result: [(' 1 ', ' 2 '), (' 1 ', ' 3 '), (' 1 ', ' 4 '), (' 2 ', ' 3 '), (' 2 ', ' 4 '), (' 3 ', ' 4 ')]
It can be seen from the results that the function of Itertools.combinations (ITERABLE,R) is to return the elements of each r length, and the result element is
The elements in the original list are combined, and the law of the combination is the combination of each element in the original list and the element behind it.
Also look at this function combinations_with_replacement (' ABCD ', 2)
If you change the combinations to combinations_with_replacement, then
The result of the printing is:
[(' 1 ', ' 1 '), (' 1 ', ' 2 '), (' 1 ', ' 3 '), (' 1 ', ' 4 '), (' 2 ', ' 2 '), (' 2 ', ' 3 '), (' 2 ', ' 4 '), (' 3 ', ' 3 '), (' 3 ', ' 4 '), (' 4 ', ' 4 ')]
Obviously the starting position of the combinations_with_replacement () combination is each element of its own
3.4
Itertools.compress (data, selectors)
The data is filtered by the selector selectors, and then only the data that passes the selectors judgment to true is returned.
Instance:
Import itertoolspartlist1= ' 1234 ' Print (List (itertools.compress (partlist1,[1,1,1,0))) results are: [' 1 ', ' 2 ', ' 3 ']
3.5
Itertools.count (Start, step)
Starting with start, each element is added with step. The step default value is 1.
Count---10 11 12 13 14 ...
We recommend that you do not use this function to test, do not ask me why, I have restarted five times.
3.6
Itertools.cycle (iterable)
After iterating to the last element of the sequence p, start again from the first element of P. Infinite iterations
As with the 3.5 recommendation, they are infinite iterations
3.7
Itertools.repeat (object[, Times])
Take a look at the third infinite iteration of the function, repeat the Objext times and then stop
Instance:
Import itertoolspartlist1= ' 1234 ' Print (List (itertools.repeat (partlist1,2)) runs the result: [' 1234 ', ' 1234 ']
3.8
Itertools.dropwhile (predicate, iterable)/itertools.takewhile (predicate, iterable)
You can tell from the name of the function that predicate is the condition, and when predicate is true, the arguments in the iterator are thrown away, until
predicate is false, output all remaining content (not judging the remaining parameters)
Instance:
Import Itertoolspartlist1=[1,2,3,4,8,1]print (List (Itertools.dropwhile (lambda x:x<3,partlist1))) results are: [3, 4, 8, 1]
Obviously, the previous x<3 are all in accordance with the above, so they are filtered out, when the parameters go to 3, because 3 is not less than 3, so
The function stops, and subsequent arguments are no longer judged.
3.9
Itertools.filterfalse (predicate, iterable)
Output predicate to false all data
An example:
Import Itertoolspartlist1=[1,2,3,4,8,1]print (List (Itertools.filterfalse (lambda x:x<3,partlist1))) output is: [3, 4, 8 ]
3.10
Itertools.islice (iterable, Start, stop[, step])
This function is a slice, I have previously introduced the blog, Iterable is to intercept the object, start position
SOP End Position, the last one is an optional parameter, step step.
Instance:
Import Itertoolspartlist1=[1,2,3,4,8,1]print (List (Itertools.islice (Partlist1,2,none))) results are: [3, 4, 8, 1]
3.11
Itertools.startmap (function,iterable) returns the parameters in the iterable, which are processed by function. Import Itertoolsprint (List (Itertools.starmap (pow,[(2,3), (3,2)))) has been printed with the following results: [8, 9]
3.12
Itertools.tee (It,n)
Returns a replication iterator for n iterator it.
Learn about Python, a sophomore, and be interested in making friends. qq:904727147
Python iteration,itertools (Python iterator, Itertool personal summary)