Python and the iteration related to two next () function and iter ()

  next():

  next () Returns an iterator's next project

  next syntax:

next(iterator[,dafault])

  iterator - iterables

  default - Optional, used to set the default return value when there is no next element, if not set, and no next element is triggered StopIteration exception.

  eg:

. 1  # ! / Usr / bin / Python 
2  # - * - Coding: UTF-. 8 - * - 
. 3   
. 4  # is first obtained Iterator objects: 
. 5 IT = ITER ([. 1, 2,. 3,. 4,. 5 ])
 . 6  # cycles : 
. 7  the while True:
 . 8      the try :
 . 9          # next value is obtained: 
10          X = next (IT)
 . 11          Print (X)
 12 is      the except StopIteration:
 13 is          # encountered StopIteration loop exits 
14          BREAK

  

  process ():

  ITER () function is used to generate an iterator

  iter syntax:

iter(object[, sentinel])

  object - a collection of objects to support iteration.

  sentinel - If the second parameter is passed, then the object must be an object parameter (e.g., function) of a call, this time, creates an iterator ITER objects, each call the iterator object the __next __ () when the method will be called object.

  eg:

>>>lst = [1,2,3]
>>>for i in iter(lst):
            print(i)
...
...
1
2
3    

 

  

Guess you like

Origin www.cnblogs.com/chester-cs/p/11547030.html