#!/usr/bin/env python
# _*_ coding=utf-8 _*_
a=[1,2,3,4,5,1,2,1,3,1,5,2,3,4,5]
first_pos=0
n=a.count(2) #计算列表中'2'的个数
print 'count:',n
for i in range(n): #循环次数
new_list=a[first_pos:] #初始第一次循环列表
print 'new list:',new_list
next_pos=new_list.index(2)+1 #当次列表中'2'的位置
print 'find pos :',first_pos+new_list.index(2) #当次'2'的位置
first_pos +=next_pos #下一次切片起始位置
另一种实现、
#!/usr/bin/env python
# _*_ coding=utf-8 _*_
a=[1,2,3,4,5,1,2,1,3,1,5,2,3,4,5]
pos =0
for i in range(a.count(2)):
if pos==0:
pos = a.index(2)
else:
pos = a.index(2,pos+1)
print pos
利用list.index(value,start,stop)