合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
`enumerate` 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。 ~~~ class enumerate(object) | enumerate(iterable[, start]) -> iterator for index, value of iterable | | 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. | enumerate is useful for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ... ~~~ enumerate 对象是迭代器 例: ~~~ l = ['a', 'b', 'c'] e1 = enumerate(l) # <enumerate object at 0x0000000003218708> e2 = enumerate(l, start=3) # <enumerate object at 0x0000000003218438> for k, v in e1: print(k, v) # 0 a -- 1 b -- 2 c for k, v in e2: print(k, v) # 3 a -- 4 b -- 5 c ~~~ ~~~ >>> from collections import Iterator >>> isinstance(enumerate([1, 2, 3]), Iterator) True ~~~