Skip to content

Instantly share code, notes, and snippets.

@azhai
Last active December 19, 2015 11:19
Show Gist options
  • Save azhai/5946654 to your computer and use it in GitHub Desktop.
Save azhai/5946654 to your computer and use it in GitHub Desktop.
Python列表去重复
# -*- coding:utf-8 -*-
#用于Python List类型的去重复,并保持元素原有次序
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)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment