In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.