Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Python iterator and generator

Shulou Source: shulou.com Published: 2022-06-01 10:50:50 09月27日 Update

This article mainly explains "Python iterator and generator how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python iterator and generator how to achieve" it!

Iteration

Iteration is a way to access collection elements. An iterator is an object that remembers the location of the traversal. The iterator object is accessed from the first element of the collection until all elements have been accessed. Iterators can only move forward, not backward.

Iterable object

There are several types of data that act directly on the for loop:

One is the collection data type, such as list, tuple, dict, set, str, etc.

One is generator, including generators and generator function with yield.

These objects that can act directly on the for loop are collectively referred to as iterable objects: Iterable.

Determine the iterable object isinstance

Use the built-in function isinstance () to determine whether it is an object of Iterable

From collections import Iterablestr = "Hello Python" print (isinstance (str,Iterable)) dir

Get all the methods of this object through the built-in function dir () and check whether there is a'_ _ iter__'

Str = 'Hello Python' print (' _ iter__' in dir (str)) iterator

An object that can be called by the next () function and constantly returns the next value is called an iterator: Iterator.

Iter () function

Generators are all Iterator objects, but list, dict, and str are Iterable, but not Iterator. To change an Iterable such as list, dict, str into Iterator, you can use the iter () function:

Generator

Another way to create an iterator is to use a generator, which is a function that is special in that its return statement is replaced by a yield statement.

For i in range (1,10,1): print (I) def customRange (star, stop, step): X = star while x < stop: yield x x + = stepfor i in customRange (1,10,0.5): print (I) generator expression (Generator Expression)

The expression of the organ is written as follows:

Generator = (for the operation of the item for item in iterable object) Thank you for reading, the above is the "Python iterator and generator how to achieve" the content, after the study of this article, I believe you have a deeper understanding of the Python iterator and generator how to achieve this problem, the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Iteration generator generation object function element learning action content that is data way type expression statement loop special continuous location writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information OPPO Reno macOS Shulou Technology