In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the use of Python iter ()". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the use of Python iter ()"?
First, go to the code and learn the usage
We are all familiar with iter (obj), which returns an iterator or an error if obj is not an iterable object. But in fact, if you look closely at the official documentation, you will find that the iter () method actually accepts two parameters, as described in the following document
Iter (object [, sentinel])
Sentinel is translated as Sentinel in English.
The sentinel parameter is optional, and when it exists, object no longer passes in an iterable object, but a callable object, which, in popular terms, is an object that can be called through (), and sentinel, like its translation, is a "sentry". At that time, when the callable object returns the value of this "sentry", the loop ends and the "sentry" is not output.
It may be a little difficult to understand, as illustrated by a simple requirement, which is as follows:
Think of a number in the range of [1, 10], and then the code starts to be random, stops when it is random to the desired number, and sees how many times the code needs to be random each time.
Implementation analysis: it should look simple, random, and then add an if judgment, but it's easier to implement it with iter (). The implementation code is as follows:
From random import randintdef guess (): return randint (0,10) num = "5for i in iter (guess, 5): print ("% s guess, guess number:% s "% (num, I)) num + = exception StopIteration is thrown when guess returns 5, but the for loop handles the exception, that is, it ends the loop.
Let's take a look at the documents
These two parameters are also described in detail in the document, which is explained in sections as follows:
The first argument is interpreted very differently depending on the presence of the second argument.
The first parameter has different meanings according to the second parameter
Without a second argument, object must be a collection object which supports the iteration protocol (the _ iter_ () method), or it must support the sequence protocol (the _ getitem_ () method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised.
If there is no second parameter, object (that is, the first parameter) is a collection object that supports the iterator protocol (which implements the _ iter_ () method), or the sequence protocol (which implements the _ getitem_ () method) and indexes starting at 0. If it does not support any of them, a TypeError exception is thrown
To put it simply, if there is no second parameter, it is the more familiar usage. The code example is as follows:
In [5]: iter ("123") Out [5]: In [6]: iter ([1,2] 3]) Out [6]: In [7]: iter (123)-TypeError Traceback (most recent call last) in ()-> 1 iter (123) TypeError: 'int' object is not iterable
Let's take a look at the case with the second parameter.
If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its _ next_ () method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
If the second parameter sentinel,object is given, it must be a callable object, which does not have any parameters. When the return value of the callable object is equal to the value of sentinel, the exception of StopIteration is thrown, otherwise the current value is returned. (if it is difficult to understand callable objects here, it can be understood as a function, which makes it easier to figure it out.)
Instructions are also given in the documentation for the applicable scenarios for this usage:
One useful application of the second form of iter () is to build a block-reader. For example, reading fixed-width blocks from a binary database file until the end of file is reached:
For the second parameter, a useful scenario is to create a blokc-reader, which interrupts the read according to the condition. For example, read a fixed-width block from a binary database file until it reaches the end of the file. The code example is as follows:
From functools import partialwith open ('mydata.db',' rb') as f: for block in iter (partial (f.read, 64), baked'): process_block (block) so far, I believe you have a better understanding of "what is the use of Python iter ()". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.