Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正如这里有人指出的那样,为了在列表中获取最大重复项,可以使用:
>>> 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, 5]
有什么帮助吗??
问候...
>>> 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]