Iterator
Iteration is one of the most powerful features of Python and is a way to access the elements of a collection. .
An iterator is an object that can remember the position of the traversal.
The iterator object starts accessing from the first element of the collection until all elements have been accessed. Iterators can only go forward and not backward.
Iterators have two basic methods: iter() and next().
String, list, or tuple objects can all be used to create iterators:
List comprehensions can become more complex --- for example, it can contain nested loops, or it can be Written as a series of for clauses. (Here is just a brief introduction, I will talk about this syntax issue later)
For example: build a x + y connected table, put each x in one string and each y in another string connect them. It effectively collects the permutation of characters in two strings:
List comprehension expressions can often be made more compact, often by indenting them and simplifying iteration type; for more related work, a simple for statement may be easier to understand and easier to modify in the future.
Other iteration environments
List comprehensions, in membership tests, map built-in functions, and built-in functions like sorted and zip calls also use the iteration protocol. When applied to a temporary file, these iterators using file objects are automatically scanned:
The above is the detailed content of Detailed explanation of examples of iterator generators in python. For more information, please follow other related articles on the PHP Chinese website!