python 列表中重复项的索引 [英] Index of duplicates items in a python list

查看:69
本文介绍了python 列表中重复项的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我如何在 python 列表中获取重复项的索引位置?我试过这样做,它一直只给我列表中该项目第一次出现的索引.

Does anyone know how I can get the index position of duplicate items in a python list? I have tried doing this and it keeps giving me only the index of the 1st occurrence of the of the item in the list.

List = ['A', 'B', 'A', 'C', 'E']

我想要它给我:

index 0: A   
index 2: A

推荐答案

您希望将可选的第二个参数传递给 index,即您希望索引开始查找的位置.找到每个匹配项后,将此参数重置为找到的匹配项之后的位置.

You want to pass in the optional second parameter to index, the location where you want index to start looking. After you find each match, reset this parameter to the location just after the match that was found.

def list_duplicates_of(seq,item):
    start_at = -1
    locs = []
    while True:
        try:
            loc = seq.index(item,start_at+1)
        except ValueError:
            break
        else:
            locs.append(loc)
            start_at = loc
    return locs

source = "ABABDBAAEDSBQEWBAFLSAFB"
print(list_duplicates_of(source, 'B'))

打印:

[1, 3, 5, 11, 15, 22]

您可以在一次遍历源中一次找到所有重复项,方法是使用 defaultdict 保留任何项目的所有看到位置的列表,并返回那些多次看到的项目.

You can find all the duplicates at once in a single pass through source, by using a defaultdict to keep a list of all seen locations for any item, and returning those items that were seen more than once.

from collections import defaultdict

def list_duplicates(seq):
    tally = defaultdict(list)
    for i,item in enumerate(seq):
        tally[item].append(i)
    return ((key,locs) for key,locs in tally.items() 
                            if len(locs)>1)

for dup in sorted(list_duplicates(source)):
    print(dup)

打印:

('A', [0, 2, 6, 7, 16, 20])
('B', [1, 3, 5, 11, 15, 22])
('D', [4, 9])
('E', [8, 13])
('F', [17, 21])
('S', [10, 19])

如果你想针对同一个源对各种key做重复测试,你可以使用functools.partial创建一个新的函数变量,使用部分完整"的参数列表,即指定seq,但省略要搜索的项目:

If you want to do repeated testing for various keys against the same source, you can use functools.partial to create a new function variable, using a "partially complete" argument list, that is, specifying the seq, but omitting the item to search for:

from functools import partial
dups_in_source = partial(list_duplicates_of, source)

for c in "ABDEFS":
    print(c, dups_in_source(c))

打印:

A [0, 2, 6, 7, 16, 20]
B [1, 3, 5, 11, 15, 22]
D [4, 9]
E [8, 13]
F [17, 21]
S [10, 19]

这篇关于python 列表中重复项的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆