`
ryan.liu
  • 浏览: 134210 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics