python - 显示列表中每个字符串的索引
问题描述
我希望在列表中找到每个整数的索引(我将其转换为字符串,因为我不知道任何替代方式)。
例如,我有一个列表:
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
解决方案
我想你正在寻找enumerate()
a = ['apple', 'ball', 'cat']
for i, it in enumerate(a):
print(i)
>>> output
0
1
2
推荐阅读
- sql - 在多行中的逗号之间分布表达式
- django - 使用比萨将html转换为pdf
- aem - SlingModels 中的异常处理并在 AEM 中使用 Pojos?
- python - 定期切割图形并使用最终图形找到整个图形的平均值
- c++ - 检测何时使用 WM_PARENTNOTIFY 将子 CButton 控件添加到 CDialog
- rust - 如何使 clap 的参数的 value_names 可选?
- python - 如何部署仅包含 python 函数的机器学习模型
- jquery - 垂直轮播,在大屏幕上有多个项目,但在移动视图中使用默认引导程序的单个图像
- amazon-web-services - 使用 cloudformation 将 IAM 角色附加到 Neptune 集群
- c++ - 缩放和插值数组