python 函数 —— enumerate()
2017-05-08 20:15
288 查看
对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值,enumerate多用于在for循环中得到计数
如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写:
enumerate()
使用
如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写:In [69]: a=list('thie is a test') In [70]: a Out[70]: ['t', 'h', 'i', 'e', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't'] In [71]: for i,j in enumerate(a): ...: print(i,j) ...: 0 t 1 h 2 i 3 e 4 5 i 6 s 7 8 a 9 10 t 11 e 12 s 13 t
enumerate还可以接收第二个参数,用于指定索引起始值,如:
In [72]: for i,j in enumerate(a, 6): ...: print(i,j) ...: ...: 6 t 7 h 8 i 9 e 10 11 i 12 s 13 14 a 15 16 t 17 e 18 s 19 t
相关文章推荐
- python的enumerate()函数
- Python 之 enumerate()内建函数
- Python序列类型常用函数练习:enumerate() reversed() sorted() zip()
- Python函数-enumerate()
- python字符串内建函数操作实例(cmp、str、enumerate、zip等)
- python的enumerate()函数
- Python enumerate() 函数
- Python内建函数:enumerate
- python之enumerate()函数
- Python---高级函数map, filter, zip, enumerate等的用法
- 【python】浅谈enumerate 函数
- python 函数-enumerate的用法
- python中的函数:zip ,map,enumerate
- python enumerate 函数用法
- python中的enumerate()函数用法
- python之enumerate()函数的探究
- python进阶教程之循环相关函数range、enumerate、zip
- Python中enumerate()函数的用法
- python之函数enumerate()
- Python enumerate函数无法顺序遍历集合