求列表中指定元素的位置

1 #!/usr/bin/env python
2 #_*_ coding:utf-8 _*_
3 name  = ['hello', 'world', 'a', 'b', 'c', 1, 2, 3, 'hello', 'world', 'a', 'b', 'c', 1, 2, 3]
4 first_pos = 0
5 for i in range(name.count(2)):
6     new_list = name[first_pos:]
7     next_pos = new_list.index(2) + 1
8     print 'find ', first_pos + new_list.index(2)
9     first_pos += next_pos

 

posted @ 2018-01-08 19:10  zhang.ning  阅读(512)  评论(0编辑  收藏  举报