Python列表去重复


Python代码
  1. def unique_list(seq, excludes=[]):   
  2.     """   
  3.     返回包含原列表中所有元素的新列表,将重复元素去掉,并保持元素原有次序  
  4.     excludes: 不希望出现在新列表中的元素们  
  5.     """  
  6.     seen = set(excludes)  # seen是曾经出现的元素集合   
  7.     return [x for x in seq if x not in seen and not seen.add(x)]  

相关内容