Created
October 25, 2015 04:32
-
-
Save GitMoIO/50f14569739ab12be2bb to your computer and use it in GitHub Desktop.
From http://www.peterbe.com/plog/uniqifiers-benchmark
Various functions to uniqify a list in python. Some preserve the orders other don't.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f1(seq): | |
# not order preserving | |
set = {} | |
map(set.__setitem__, seq, []) | |
return set.keys() | |
def f2(seq): | |
# order preserving | |
checked = [] | |
for e in seq: | |
if e not in checked: | |
checked.append(e) | |
return checked | |
def f3(seq): | |
# Not order preserving | |
keys = {} | |
for e in seq: | |
keys[e] = 1 | |
return keys.keys() | |
def f4(seq): | |
# order preserving | |
noDupes = [] | |
[noDupes.append(i) for i in seq if not noDupes.count(i)] | |
return noDupes | |
def f5(seq, idfun=None): | |
# order preserving | |
if idfun is None: | |
def idfun(x): return x | |
seen = {} | |
result = [] | |
for item in seq: | |
marker = idfun(item) | |
# in old Python versions: | |
# if seen.has_key(marker) | |
# but in new ones: | |
if marker in seen: continue | |
seen[marker] = 1 | |
result.append(item) | |
return result | |
def f6(seq): | |
# Not order preserving | |
set = Set(seq) | |
return list(set) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment