Python built-in letter Iter

Source: Internet
Author: User
English documents:

ITER (object[, Sentinel])

Return an Iterator object. The first argument is interpreted very differently depending on the presence of the second argument. 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 an integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If The second argument, Sentinel, is given and then object must be a callable object. The iterator created in this case would call object with no arguments for each call to its__next__ () method; If the value returned is equal to Sentinel, Stopiteration would be raised, otherwise the value would be returned.

One useful application of the second form of ITER () is to read lines of a file until a certain line is reached. The following example reads a file until the ReadLine () method returns an empty string:

With open (' mydata.txt ') as Fp:for line in ITER (Fp.readline, "):
Process_line (line)

Description

1. Function function returns an object that can be iterated.

2. When the second parameter is not provided, the first argument must be a collection (dictionary, collection, immutable collection) that supports an iterative protocol (that is, the __iter__ () method is implemented), or a sequence protocol (that is, the __getitem__ () method is implemented, and the method receives an integer parameter starting from 0) Sequence (tuple, list, string), otherwise an error will be expected.

>>> A = iter ({' A ': 1, ' B ': 2}) #字典集合 >>> a
 
  
   
  >>> Next (a) ' A ' >>> next (a) ' B ' >>> Next (a) Traceback (most recent call last):  File "
  
   
    
   ", line 1, in 
   
    
     
        next (a) Stopiteration >>> a = iter (' ABCD ') #字符串序列 >>> a
    
     
      
     >>> Next (a) ' a ' >>> Next (a) ' B ' >>> next (a) ' C ' >>> next (a) ' d ' >>> next (a) Traceback (most recent):  File "
     
      
       
      ", line 1, in 
      
       
         next (a) stopiteration 
       
     
      
    
     
   
    
  
   
 
  

3. When Sentinel provides the second parameter, the first argument must be a callable object. Creates an iterative object that invokes the callable object when the __next__ method is called, and throws an Stopiteration exception when the return value and the Sentinel value are equal, terminating the iteration.

# definition Classes >>> class Itertest:     def __init__ (self):        self.start = 0        self.end = ten    def get_next_value ( Self): Current        = Self.start        If current < self.end:            Self.start + = 1        else:            raise stopiteration        return current>>> itertest = itertest () #实例化类 >>> a = iter (itertest.get_next_value,4) # Itertest.get_next_value as callable object with Sentinel value 4>>> a
 
  
   
  >>> Next (a) >>> next (a) >>> Next (a) >>> next (a) >>> next (a) #迭代到4终止Traceback (most recent):  File "
  
   
    
   ", line 1, in 
   
    
     
        next (a) stopiteration
   
    
  
   
 
  
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.