关闭

python 去掉list中的重复元素

1534人阅读 评论(0) 收藏 举报
def f1(seq): 
    # not order preserving 
    set = {} 
    map(set.__setitem__, seq, []) 
    return set.keys()

def f2(seq): 
    # order preserving 
    checked = [] 
    for e in seq: 
        if e not in checked: 
            checked.append(e) 
    return checked

def f3(seq): 
    # Not order preserving 
    keys = {} 
    for e in seq: 
        keys[e] = 1 
    return keys.keys()

def f4(seq): 
    # order preserving 
    noDupes = [] 
    [noDupes.append(i) for i in seq if not noDupes.count(i)] 
    return noDupes

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 f6(seq): 
    # Not order preserving     
    set = Set(seq) 
    return list(set)


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70487次
    • 积分:846
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:42篇
    • 译文:0篇
    • 评论:3条
    文章分类