列表去重的几种方式

from functools import reduce
 
 
class StringReverse(object):
    '''
    列表去重,并按照原来的顺序排序
    '''
 
    # 1.利用set方法和sort方法,原序
    def string_duplicate_1(self, s):
        new_s = list(set(s))  # set无序
        new_s.sort(key=s.index)
        return new_s
 
    # 2.用列表中的元素作为字典中的key生成一个新字典,然后获取字典的key,非原序
    def string_duplicate_2(self, s):
        a = {}
        # fromkeys(s,v)该方法的功能是生成一个字典,字典的key是 s中的值,s为可迭代对象,可以为str,tuple,list,set,dict,v为每一个key的值,默认为None
        return a.fromkeys(s).keys()
 
    # 3.利用defaultdict, 非原序
    def string_duplicate_3(self, s):
        # 按照之前的顺序
        from collections import defaultdict
        a = defaultdict()
        for x in s:
            a[x] = 0
        return a.keys()
 
    # 4.最简单的循环,添加入新的列表,如果新列表中没有相同元素,则加入。原序
    def string_duplicate_4(self, s):
        new_s = []
        for x in s:
            if x not in new_s:
                new_s.append(x)
        return new_s
 
    # 5.利用itertools的groupby方法。非原序
    def string_duplicate_5(self, s):
        from itertools import groupby
        s.sort()
        new_groupby = groupby(s)
        new_s = []
        for x, y in new_groupby:
            new_s.append(x)
        return new_s
 
    # 6.reduce方法。非原序
    def string_duplicate_6(self, s):
        return reduce(lambda x, y: x if y in x else x + [y], [[], ] + s)
 
 
if __name__ == "__main__":
    stringReverse = StringReverse()
    s = [1, 3, 2, 34, 4, 6, 6, 7, 1, 4, 8, 98]
    print("string_duplicate_1", stringReverse.string_duplicate_1(s))
    print("string_duplicate_2", stringReverse.string_duplicate_2(s))
    print("string_duplicate_3", stringReverse.string_duplicate_3(s))
    print("string_duplicate_4", stringReverse.string_duplicate_4(s))
    print("string_duplicate_5", stringReverse.string_duplicate_5(s))
    print("string_duplicate_6", stringReverse.string_duplicate_6(s))
 

 

posted @ 2021-04-26 17:00  Fleeting__Time  阅读(184)  评论(0编辑  收藏  举报