我想修改这个列出重复项的定义,使其列出重复值的索引。另外,我希望它列出所有的重复项,这意味着对于a = [1,2,3,2,1,5,6,5,5,5],结果应为duplicate_indexes = [3,4,7,8,9]。以下是定义:
def list_duplicates(seq):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
seen_twice = set( x for x in seq if x in seen or seen_add(x) )
# turn the set into a list (as requested)
return list( seen_twice )
a = [1,2,3,2,1,5,6,5,5,5]
list_duplicates(a) # yields [1, 2, 5]