考慮すべきいくつかの相対的なタイミング:
from collections import Counter
from collections import defaultdict
mylist = [20, 30, 25, 20, 30]
def f1():
seen = set()
dups = set()
for x in mylist:
if x in seen:
dups.add(x)
seen.add(x)
max_dups = max(dups)
def f2():
max(x for x in mylist if mylist.count(x) > 1)
def f3():
max(k for k,v in Counter(mylist).items() if v>1)
def f4():
dd = defaultdict(int)
for i in mylist:
dd[i] += 1
max(i for i in dd if dd[i] > 1)
def f5():
d = dict.fromkeys(mylist, 0)
for i in mylist:
d[i] += 1
max(i for i in d if d[i] > 1)
cmpthese([f1,f2,f3,f4,f5])
プリント:
rate/sec f3 f4 f5 f2 f1
f3 93,653 -- -63.3% -73.0% -79.2% -83.6%
f4 255,137 172.4% -- -26.3% -43.3% -55.3%
f5 346,238 269.7% 35.7% -- -23.1% -39.3%
f2 450,356 380.9% 76.5% 30.1% -- -21.0%
f1 570,419 509.1% 123.6% 64.7% 26.7% --
だから賢く選ぶ