码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-28 11:54:37      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:列表   span   bsp   存在   style   enumerate   ==   color   索引   

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]]

 

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

标签:列表   span   bsp   存在   style   enumerate   ==   color   索引   

原文地址:http://www.cnblogs.com/wang666/p/7908508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!