Python:获取列表中每个项目的索引,其中包含重复项

2024-09-28 22:00:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何获取列表中每个项(包括重复项)的索引?据我所知列表.索引只会给我一个重复项的第一个索引,像这样。你知道吗

registros = ["Celda", "Celda", "Test", "Celda", "Celda", "Celda"]
    for item in registros:
        index_found = registros.index(item)
        print(str(index_found) + " " + dato)

输出:

0 Celda
0 Celda
2 Test
0 Celda
0 Celda
0 Celda

那么,我该如何(尽可能简单地)获得所需的输出呢?地址:

0 Celda
1 Celda
2 Test
3 Celda
4 Celda
5 Celda

Tags: intest列表forindex地址itemprint