
Python 中查找列表中特定元素的所有索引的方法
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本文介绍了如何在Python中高效地找到列表里某个特定值的所有出现位置,并提供了具体的实现代码示例。
以下是经过调整的代码:
```python
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
name = [hello, world, a, b, c, 1, 2, 3, hello, world, a, b, c, 1, 2, 3]
first_pos = 0
for i in range(name.count(2)):
new_list = name[first_pos:]
next_pos = new_list.index(2) + 1
print(find, first_pos + new_list.index(2))
```
注意:代码中的字符串hello, world, 和变量名a,b,c需要使用引号括起来,以便于Python识别为字符串类型。原始代码中这些元素未加引号可能是因为它们被当作占位符或错误输入,请根据实际需求调整。
全部评论 (0)
还没有任何评论哟~


