利用枚举找到列表中重复元素的索引

 

list_3 = []
list_1 = ['2', '5', '6', '11', '2', '535', '2', '2' ]
print(list_1)
s = '2'
for item in enumerate(list_1):
    if item[1] == str(s):
        list_3.append(item[0])
print(list_3)

运行结果:

['2', '5', '6', '11', '2', '535', '2', '2']
[0, 4, 6, 7]

枚举:

for i in enumerate(('a','b','c')):
    print (i)

(0, 'a')
(1, 'b')
(2, 'c')

for i,j in enumerate(('a','b','c')):
    print (i,j)

运行结果:

0 a
1 b
2 c

 

list_3 = []
list_4 = []
list_1 = ['2', '5', '6', '11', '2', '5', '2', '2' ]
s = '2'
for i,j in enumerate(list_1):
    print (i,j)
for i, j in enumerate(list_1):
    if j==s:
        list_3.append(i)
print(list_3)
for item in enumerate(list_1):
    if item[1] == str(s):
        list_4.append(item[0])
print(list_4)

 

posted on 2018-12-10 19:55  一杯明月  阅读(961)  评论(0编辑  收藏  举报