Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下列表:
lista = [1,2,3,5,0,5,6,0]
我知道这print(lista.index(0))将打印找到数字的第一个索引,即 4
print(lista.index(0))
如何让它打印下一个应该是 7 等的索引?
经典的方法是建立一个索引列表:
例如:
>>> indices = [i for i, v in enumerate(a) if v == 0] >>> print indices [4, 7]
当然 - 这可以变成一个生成器表达式。但简而言之 - 使用enumerate就是你所追求的。
enumerate