python列表元素去重_python列表里的字典元素去重

去重

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值