Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand iterators and generators in Python Foundation

Shulou Source: shulou.com Published: 2022-06-01 21:36:06 11月13日 Update

This article introduces how to understand the iterators and generators in the Python foundation, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. Iterator

Iteration is one of the most powerful features of Python and 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.

Iterators have two basic methods: iter () and next ().

Strings, lists, or tuple objects can be used to create iterators:

> > list= > > it = iter (list) # create iterator object > print (next (it)) # next element of output iterator 1 > print (next (it)) 2 >

Iterator objects can be traversed using regular for statements:

#! / usr/bin/python3 list= [1 list 2, 3 usr/bin/python3 list= 4] it = iter (list) # create an iterator object for x in it: print (x, end= "")

Execute the above procedure, and the output is as follows:

1 2 3 4

You can also use the next () function:

#! / usr/bin/python3 import sys # introduces the sys module list= [1mem2Jing 3Jing 4] it = iter (list) # to create an iterator object while True: try: print (next (it)) except StopIteration: sys.exit ()

Execute the above procedure, and the output is as follows:

12342. Create an iterator

Using a class as an iterator requires the implementation of two methods, _ _ iter__ () and _ _ next__ (), in the class.

If you already know object-oriented programming, you know that every class has a constructor. The constructor of Python is _ _ init__ (), which is executed when the object is initialized.

For more information: Python3 object oriented

The _ _ iter__ () method returns a special iterator object that implements the _ _ next__ () method and marks the completion of the iteration with a StopIteration exception.

The _ _ next__ () method (next () in Python 2) returns the next iterator object.

Create an iterator that returns a number, with an initial value of 1, incrementing by 1:

1234

The output of the execution is:

123453.StopIteration

The StopIteration exception is used to mark the completion of the iteration to prevent the occurrence of an infinite loop. In the _ _ next__ () method, we can set the StopIteration exception to be triggered after the specified number of loops to end the iteration.

Stop execution after 20 iterations:

Class MyNumbers: def _ iter__ (self): self.a = 1 return self def _ next__ (self): if self.a n): return yield an a, b = b, a + b counter + = 1f = fibonacci (10) # f is an iterator The generator returns the generated while True: try: print (next (f), end= "") except StopIteration: sys.exit ()

Execute the above procedure, and the output is as follows:

0 1 1 2 3 5 8 13 21 34 55 on how to understand the iterators and generators in the Python foundation is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Iterations objects generators generators functions methods outputs results elements content more programs runs foundations two location identification help loops different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux OPPO Reno Microsoft Shulou Technology