Home > Backend Development > Python Tutorial > Detailed explanation of examples of iterator generators in python

Detailed explanation of examples of iterator generators in python

零下一度
Release: 2017-07-19 09:16:17
Original
1118 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template