I. Iterative---and-----sequences
An iteration is anything you can traverse with a for loop in Python. An iterative means that this can be traversed, and anything that can be traversed is iterative.
A sequence is a very common type of iteration , in which lists, tuples, and strings are sequences.
The sequence is iterative, and he has some specific set of features. They can be indexed starting from 0, ending with a length smaller than the sequence, they have a length and they can be sliced. Lists, tuples, strings, and all the other sequences work like this.
A lot of things in Python are iterative, but not all of the things that can be iterated are sequences . collections, dictionaries, files, and generators are all iterative, but they are not sequences.
The sequence can be traversed using the index of the subscript, but this method only applies to the sequence.
Two. Iterator Driver for Loop
Python for loop uses iterators
An iterator is something that can drive an iterative object. You can get an iterator from any iteration object, or you can use an iterator to iterate through its iterations manually.
We can use Python's built-in iter functions to access these iterators, and passing an iterator to the iter function always returns us an iterator, no matter what type of iterator we are using.
Go to http://python.jobbole.com/89181/ essence at the end of the text
1 >>> numbers = {1, 2, 3, 5, 7}2 >>> coordinates = (4, 5, 7)3"Hello there"
1 >>> iter (numbers)2 <set_iterator object at 0x7f2b9271c860>3 > >> iter (coordinates)4 <tuple_iterator object at 0x7f2b9271ce80>5 >>> iter (words) 6 <str_iterator object at 0x7f2b9271c860>
Get an iterator from an iterative object, using the ITER function
Once we have an iterator, the thing we can do is to get the next item by passing it to the built-in next function.
Iterators are stateful, which means that once you consume one item from them, it disappears.
Three. Generator is an iterator
The generator is an iterator, which means that you can call next on the generator to get their next item
The generator can also be iterated,
Each iterator in Python is iterative, meaning that you can iterate through the iterator.
Because iterators are also iterative, you can use built-in next functions to get iterators from an iterative object
Keep in mind that when we call on an iterative object iter , it returns an iterator to us.
When we call it on an iterator iter , it will give us the return of its own iterator which is iterative, and all iterators are their own iterators.
Let us recall some of these words.
- An iterative object is something you can iterate over.
- An iterative object is a proxy that actually traverses an iterative object
In addition, iterators are also iterative in Python, and they act as their own iterators.
So iterators are iterative, but they do not have a number of features that can be iterated over objects.
You can think of iterators as lazy iterators , which are used once , which means they can only iterate through
All iterator protocols
Let's define how iterators work from a Python perspective.
An iterative object can be passed to iter a function to obtain an iterator for them.
Iterators:
- Can be passed to the
next function, it will give the next item, and if there is no next item, it will throw an StopIteration exception
- Can be passed to
iter a function, it returns an iterator of its own
These statements are, in turn, correct:
- Anything that can be passed without throwing an
TypeError exception can be iter iterated.
- Anything that can be passed without throwing an
TypeError exception next is an iterator
- When passed to
iter , anything that returns itself is an iterator
This is the iterator protocol in Python.
The content is not fully understood
How iterators can improve your code
Four. Part of the consumption iterator
Python Knowledge points