In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.