Python面试题 之 Uniquify a list 不使用set去除一个列表中的重复项,并且保持原有的排列顺序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010870545/article/details/48340993
def uniquify_a_list(ls):
    _ = lambda e, ls=[] : None if e in ls else ls.append(e) or ls
    return map(_, ls)[0]     


def uniquify_a_sorted_list(ls):
    # two pointers solution (learn from LeetCode)
    # initial position to compare with
    j=0
    for i, x in enumerate(ls):
        if ls[j] != x:
            # if x not equals to ls[j], fill x into the next position
            j+=1
            ls[j] = x
    return ls[:j+1] # total j+1 unique elements
            

猜你喜欢

转载自blog.csdn.net/u010870545/article/details/48340993
今日推荐