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 one1 two2 threefor 循环使用 enumerate
>>>seq = ['one', 'two', 'three']>>> for i, element in enumerate(seq):... print i, element... 0 one1 two2 three文章来自 http://www.96net.com.cn