打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python3内置函数enumerate()

enumerate()是Python的内置函数。

 1 help(enumerate) 2 Help on class enumerate in module builtins: 3 class enumerate(object) 4   enumerate(iterable[, start]) -> iterator for index, value of iterable 5   #enumerate(可迭代变量[,开始下标])->返回迭代变量的索引和值。  (注意,字典和集合也能使用,取位置作为索引) 6   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. 7 enumerate is useful for obtaining an indexed list:  8       (0, seq[0]), (1, seq[1]), (2, seq[2]), ... 9  10  Methods defined here:11   12 __getattribute__(self, name, /)13       Return getattr(self, name).14   15 __iter__(self, /)16       Implement iter(self).17   18 __new__(*args, **kwargs) from builtins.type19      Create and return a new object.  See help(type) for accurate signature.20 21 __next__(self, /)22      Implement next(self).23  24 __reduce__(...)25     Return state information for pickling.

对于一个可迭代的iterable/可遍历的对象(如列表,字符串,元组,字典和集合)。

enumerate()多用于在for循环中得到计数;enumerate()返回的是一个enumerate对象。

 1 for i in enumerate([1,2,3,4,5]):  #对列表作用 2     print(i) 3      4 (0, 1) 5 (1, 2) 6 (2, 3) 7 (3, 4) 8 (4, 5) 9 10 for i in enumerate((1,2,3)): #对元组作用11     print(i)12     13 (0, 1)14 (1, 2)15 (2, 3)16 17 for i in enumerate('help'): #对字符串作用18     print(i)19     20 (0, 'h')21 (1, 'e')22 (2, 'l')23 (3, 'p')24 25 for i in enumerate({'name':'zoe','age':18}):  #对字典作用26     print(i)27     28 (0, 'name')29 (1, 'age')30 31 for i in enumerate({12,3}): #对元组作用32     print(i)33     34 (0, 3)35 (1, 12)

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python内置函数大全
python
python循环zip与enumerate
Python中堪称神仙的6个内置函数
python 内置方法都有哪些?通过分类整合成一套简单的备忘教程
Python中最常用的15个内置函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服