码迷,mamicode.com
首页 > 编程语言 > 详细

python列表里的字典元素去重

时间:2018-11-11 15:57:31      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:cat   turn   self   none   class   iter   字典   ever   too   

去重

def list_dict_duplicate_removal():
    data_list = [{"a": "123", "b": "321"}, {"a": "123", "b": "321"}, {"b": "321", "a": "123"}]
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + data_list)


if __name__ == ‘__main__‘:
    print list_dict_duplicate_removal()

输出结果:

[{‘a‘: ‘123‘, ‘b‘: ‘321‘}]

 

python列表中元素去重的几种方式

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)   

输出结果为:

string_duplicate_1 [1, 3, 2, 34, 4, 6, 7, 8, 98]
string_duplicate_2 [1, 2, 3, 4, 98, 6, 7, 8, 34]
string_duplicate_3 [1, 2, 3, 4, 98, 6, 7, 8, 34]
string_duplicate_4 [1, 3, 2, 34, 4, 6, 7, 8, 98]
string_duplicate_5 [1, 2, 3, 4, 6, 7, 8, 34, 98]
string_duplicate_6 [1, 2, 3, 4, 6, 7, 8, 34, 98]

  

  

 

python列表里的字典元素去重

标签:cat   turn   self   none   class   iter   字典   ever   too   

原文地址:https://www.cnblogs.com/robinunix/p/9942102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!