posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

在Python的for循环中计数

Posted on 2007-12-25 21:29 ZelluX 阅读(3865) 评论(0)  编辑  收藏 所属分类: Scripting

利用enumerate

for i, obj in enumerate(list):
    print i, obj

Help on class enumerate in module __builtin__:

class enumerate(object)
 |  enumerate(iterable) -> iterator for index, value of iterable
 |
 |  Return an enumerate object.  iterable must be an other object that supports
 |  iteration.  The enumerate object yields pairs containing a count (from
 |  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 at 0xb7f35d20>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T


只有注册用户登录后才能发表评论。


网站导航: