Python列表去重复
def unique_list(seq, excludes=[]):
"""
返回包含原列表中所有元素的新列表,将重复元素去掉,并保持元素原有次序
excludes: 不希望出现在新列表中的元素们
"""
seen = set(excludes) # seen是曾经出现的元素集合
return [x for x in seq if x not in seen and not seen.add(x)]
参考:
http://www.peterbe.com/plog/uniqifiers-benchmark

浙公网安备 33010602011771号