首页 > 解决方案 > 显示列表中每个字符串的索引

问题描述

我希望在列表中找到每个整数的索引(我将其转换为字符串,因为我不知道任何替代方式)。

例如,我有一个列表:

a = [0,3,3,7,5,3,11,1] # My list
a = list(map(str, a)) # I map it to string for each integer in the list, is there any better way than this? I would like to learn

for x in a: # I then loop each str in the list
    print(a.index(x)) # here I print each index of the str

我的输出是:

0
1
1
3
4
1
6
7

我的预期输出应该是:

0
1
2
3
4
5
6
7

标签: python

解决方案


我想你正在寻找enumerate()

a = ['apple', 'ball', 'cat']

for i, it in enumerate(a):
    print(i)

>>> output
0
1
2

推荐阅读