python_获得列表中重复的项的索引

a = ['b','a', 'b', 'c', 'a', 'c','d']

b=[]
f=[]
for i in a:
    c=[]
    for item in enumerate(a):
        if item[1] ==i:
            c.append(item[0])
    b.append(c)
print(b)

for j in b:
    d=[]
    for k in j:
        d.append(a[k])
    f.append(d)
print(f)

得到

#b   [[0, 2], [1, 4], [0, 2], [3, 5], [1, 4], [3, 5], [6]]

#f   [['b', 'b'], ['a', 'a'], ['b', 'b'], ['c', 'c'], ['a', 'a'], ['c', 'c'], ['d']]

其中存在重复的小列表,可以对此列表去重

e=[] 
for i in b:
    if i not in e:
        e.append(i)
print(e) #[[0, 2], [1, 4], [3, 5], [6]]
原文地址:https://www.cnblogs.com/wang666/p/7908508.html