标签:循环应用 内建函数 应用 summer int inter python sequence nbsp
Python 内建函数enumerate() 由于这个单纯很长,不容易记住,用法还是比较广泛的,下面讲述Python内建函数enumerate()用法。
1,实例
enumerate(sequence, [start=0])
2,enumerate()用法
>>>seasons = [‘Spring‘, ‘Summer‘, ‘Fall‘, ‘Winter‘]
>>> list(enumerate(seasons))
[(0, ‘Spring‘), (1, ‘Summer‘), (2, ‘Fall‘), (3, ‘Winter‘)]
>>> list(enumerate(seasons, start=1)) # 小标从 1 开始
[(1, ‘Spring‘), (2, ‘Summer‘), (3, ‘Fall‘), (4, ‘Winter‘)]
3,在for循环应用
普通的 for 循环
>>>i = 0
>>> seq = [‘one‘, ‘two‘, ‘three‘]
>>> for element in seq:
... print i, seq[i]
... i +=1
...
0 one
1 two
2 three
for 循环使用 enumerate
>>>seq = [‘one‘, ‘two‘, ‘three‘]
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
文章来自 http://www.96net.com.cn
Python内建函数enumerate()用法及在for循环应用
标签:循环应用 内建函数 应用 summer int inter python sequence nbsp
原文地址:https://www.cnblogs.com/96net/p/9667196.html