- enumerate(sequence[, start=0]) )返回一个枚举对象,sequence必须是一个序列、迭代器或者具有具有迭代功能的对象。enumerate()是适用于获得索引集:(0, seq[0]), (1, seq[1]), (2, seq[2]), ....
- >>> i = 0
- >>> seq = ['one', 'two', 'three']
- >>> for element in seq:
- ... seq[i] = '%d: %s' % (i, seq[i])
- ... i += 1
- ...
- >>> seq
- ['0: one', '1: two', '2: three']
- >>> seq = ['one', 'two', 'three']
- >>> for i, element in enumerate(seq):
- ... seq[i] = '%d: %s' % (i, seq[i])
- ...
- >>> seq
- ['0: one', '1: two', '2: three']
- >>> def _treatment(pos, element):
- ... return '%d: %s' %(pos, element)
- ...
- >>> seq = ['one', 'two', 'three']
- >>> [_treatment(i, e1) for i, e1 in enumerate(seq)]
- ['0: one', '1: two', '2: three']
转自:http://greybeard.iteye.com/blog/1295827