# 检查列表中的所有元素是否唯一

` `>>> x = [1, 1, 1, 2, 3, 4, 5, 6, 2] >>> counter = Counter(x) >>> for values in counter.itervalues(): if values > 1: # do something` `

` `if len(x) > len(set(x)): pass # do something` `

` `>>> def allUnique(x): ... seen = set() ... return not any(i in seen or seen.add(i) for i in x) ... >>> allUnique("ABCDEF") True >>> allUnique("ABACDEF") False` `

` `>>> def allUnique(x): ... seen = list() ... return not any(i in seen or seen.append(i) for i in x) ... >>> allUnique([list("ABC"), list("DEF")]) True >>> allUnique([list("ABC"), list("DEF"), list("ABC")]) False` `

` `def unique_values(g): s = set() for x in g: if x in s: return False s.add(x) return True` `

` `import numpy as np x = [1, 1, 1, 2, 3, 4, 5, 6, 2] np.unique(x).size == len(x)` `

` `len({}.fromkeys(x)) == len(x)` `

` `len(set(x)) == len(x)` `

` `def distinct(L): if len(L) == 2: return L[0] != L[1] H = L[0] T = L[1:] if (H in T): return False else: return distinct(T)` `

` `def is_unique(lst): if not lst: return True else: return Counter(lst).most_common(1)[0][1]==1` `

` `from itertools import groupby is_unique = lambda seq: all(sum(1 for _ in x[1])==1 for x in groupby(sorted(seq)))` `

` `def is_unique(lst): if len(lst) > 1: return is_unique(s[1:]) and (s[0] not in s[1:]) return True` `

` `if tempDF['var1'].size == tempDF['var1'].unique().size: print("Unique") else: print("Not unique")` `

` ` 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 AllDifferent(s): for i in range(len(s)): for i2 in range(len(s)): if i != i2: if s[i] == s[i2]: return False return True` `