python enumerate() 使用

来源:互联网 发布:成绩管理系统c程序软件 编辑:程序博客网 时间:2024/06/11 07:06

在python中实用enumerate() function。

在对列表遍历的时候,我们使用for进行遍历,今天学习到enumerate()可以同时在使用index和value时用。

目前只在列表中使用。方法解释:

def __init__(self, iterable, start=0): # known special case of enumerate.__init__    """ Initialize self.  See help(type(self)) for accurate signature. """    pass

>>> for i,ele in enumerate(list_test):
...     print(i,ele)
...
0 dad
1 dasddaas
2 dasdsadasdasd

使用enumerate()更加有效率,更加方便。

以下在python 中的摘要:

  1. class enumerate(object) 
  2.  
  3.  |  enumerate(iterable[, start]) -> iterator for index, value of iterable  #迭代器的索引,迭代器的值
  4.  
  5.  |   
  6.  
  7.  |  Return an enumerate object.  iterable must be another object that supports 
  8.  
  9.  |  iteration.  The enumerate object yields pairs containing a count (from 
  10.  
  11.  |  start, which defaults to zero) and a value yielded by the iterable argument. 
  12.  
  13.  |  enumerate is useful for obtaining an indexed list: 
  14.  
  15.  |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ... 
  16.  
  17.  |   
  18.  
  19.  |  Methods defined here: 
  20.  
  21.  |   
  22.  
  23.  |  __getattribute__(...) 
  24.  
  25.  |      x.__getattribute__('name') <==> x.name 
  26.  
  27.  |   
  28.  
  29.  |  __iter__(...) 
  30.  
  31.  |      x.__iter__() <==> iter(x) 
  32.  
  33.  |   
  34.  
  35.  |  next(...) 
  36.  
  37.  |      x.next() -> the next value, or raise StopIteration 
  38.  
  39.  |   
  40.  
  41.  |  ---------------------------------------------------------------------- 
  42.  
  43.  |  Data and other attributes defined here: 
  44.  
  45.  |   
  46.  
  47.  |  __new__ = <built-in method __new__ of type object> 
  48.  
  49.  |      T.__new__(S, ...) -> a new object with type S, a subtype of T 

0 0