python中的enumerate()函数

在python中有一个内置函数enumerate会将数组或列表组成一个索引序列。使我们再获取索引和索引内容的时候更加方便快捷,一个很简单的案例如下:

>>> str='nihaomemem'
>>> for i,tem in enumerate(str):
...     print 'str[%d]=%s'%(i,tem)

结果如下

str[0]=n
str[1]=i
str[2]=h
str[3]=a
str[4]=o
str[5]=m
str[6]=e
str[7]=m
str[8]=e
str[9]=m

下面是python help的详细介绍

Help on class enumerate in module __builtin__:

class enumerate(object)
 |  enumerate(iterable[, start]) -> iterator for index, value of iterable
 |  
 |  Return an enumerate object.  iterable must be another object that supports
 |  iteration.  The enumerate object yields pairs containing a count (from
 |  start, which defaults to zero) and a value yielded by the iterable argument.
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

一个简单易懂的案例,可以帮助理解此函数

你可能感兴趣的:(python)