1

正如这里有人指出的那样,为了在列表中获取最大重复项,可以使用:

>>> from collections import Counter
>>> mylist = [20, 20, 25, 25, 30, 30]
>>> max(k for k,v in Counter(mylist).items() if v>1)
30

但是,如果我想获取索引而不是值怎么办?[4, 5]

有什么帮助吗??

问候...

4

1 回答 1

2
>>> from collections import defaultdict
>>> mylist = [20, 20, 25, 25, 30, 30]
>>> D = defaultdict(list)
>>> for i,x in enumerate(mylist):
        D[x].append(i)


>>> D[max(k for k,v in D.items() if len(v)>1)]
[4, 5]
于 2012-06-30T01:06:52.017 回答