去重

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列表里的字典元素去重的更多相关文章

  1. python 将列表里的字典元素合并为一个字典

    python 将列表里的字典元素合并为一个字典 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn. ...

  2. Python—列表元组和字典

    Python-列表元组和字典 列表 元组 字典 列表: 列表是Python中的一种数据结构,他可以存储不同类型的数据.尽量存储同一种类型 列表索引是从0开始的,我们可以通过索引来访问列表的值. 列表的 ...

  3. 如何在python列表中查找某个元素的索引

    如何在python列表中查找某个元素的索引 2019-03-15 百度上回复别人的问题,几种方式的回答: 1) print('*'*15,'想找出里面有重复数据的索引值','*'*15) listA ...

  4. [转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

    创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...

  5. Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

    创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...

  6. 转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

    转自:http://blog.csdn.net/business122/article/details/7536991 创建列表 sample_list = ['a',1,('a','b')] Pyt ...

  7. Python列表中的字典按照该字典下的键值进行排序

    列表中的字典按照该字典下的键值进行排序 这算是排序中比较复杂的一种情况吧,多重嵌套,按照某种规则进行排序.如下面这个json(注:这里这是该列表中的一个项): [ { "stat" ...

  8. 如何python循环中删除字典元素

    //下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...

  9. python 删除列表中的字典元素

    data={"} ,{"}]} print("before:\n\n",data,"\n\n") temp=- k= for i in da ...

随机推荐

  1. thinkphp 跳转外网代码(php通用)

    thinkphp 提供了一个重定向但是在跳转外部网站的时候就会比较麻烦 下面一种方法还不错, < ?php //重定向浏览器 header("Location: http://www. ...

  2. PHP生成ZIP压缩文件

    PHP生成ZIP压缩文件 /* * 生成zip压缩文件 * $sourceDir:被压缩的文件夹或文件 * $outFileName:输出的压缩文件名称 * */ function createZip ...

  3. php红包算法函数[优化]

    php红包算法 <?php header("Content-Type: text/html;charset=utf-8");//输出不乱码,你懂的 $total=10000; ...

  4. 平衡二叉查找树 AVL 的实现

    不同结构的二叉查找树,查找效率有很大的不同(单支树结构的查找效率退化成了顺序查找).如何解决这个问题呢?关键在于如何最大限度的减小树的深度.正是基于这个想法,平衡二叉树出现了. 平衡二叉树的定义 (A ...

  5. php中const与static的区别与使用(转)

    首先关于const 在php的类内部只可以修饰成员属性,不可以修饰方法,如下:   class Test{   const PATH = 'c/';//修饰常量   const function te ...

  6. 海康威视面试python后端题

    1. 请简述三次握手和四次挥手: 答:首先TCP是传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接,在建立TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器的指 ...

  7. PTA 7-12(图) 社交网络图中结点的“重要性”计算 最短路

    7-12(图) 社交网络图中结点的“重要性”计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的 ...

  8. mysql学习第二天函数

    -- 1.绝对值 select abs(-1)from dual -- 2.求平方根select sqrt(6)from dual -- 3.圆周率select pi()from dual -- 4. ...

  9. dubbo的rpc异常

    Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method ...

  10. 如何让button保持点击状态

    http://blog.csdn.net/u010957508/article/details/38385207 他的原理就是在代码里面: view.setSelected(true); 而其他的: ...