枚举扩展以支持手动推进或跳过索引
enumerate_skip的Python项目详细描述
这个包提供了两个迭代器,枚举跳过和枚举手册 稍微扩展一下标准库的行为。
枚举跳过已通过一个跳过方法进行扩展,以确保下一个 得到的索引与当前索引相同。单词skip可能不会 一开始是有意义的,但是这个函数是因为我不需要处理 迭代器产生的一些值,如:
for index, obj in enumerate(...): if obj.has_some_attr(): continue # do something with object and index here
如果该示例中的索引被打印给用户(或类似的内容),则 就是差距。上面的例子可以是 写成:
it = enumerate_skip(...) for index, obj in it: if obj.has_some_attr(): it.skip() continue # do something with object and index here
枚举手册 前进以增加索引:
it = enumerate_manual(...) for index, obj in it: if obj.has_some_attr(): continue # do something with object and index here it.advance()