Python 查找list中的某个元素的所有的下标方法

364人浏览 / 0人评论

如下所示:

#!/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)
 first_pos += next_pos

note:查找出列表中所有的某个元素

以上这篇Python 查找list中的某个元素的所有的下标方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

全部评论

晴天下起了小雨
2017-10-01 18:00
很喜欢,果断关注了
wjmyly7336064
2017-10-01 18:00
相当实用,赞美了
橘大佬
2017-10-01 18:00
就是有些细节再到位点就好了…