工作中遇到了求两个集合的差集,但是集合集合中包含字典,所以使用difference方法会报错,看了一些别人的博客,整理了一下。

1. 获取两个list 的交集
print list(set(a).intersection(set(b)))

2. 获取两个list 的并集
print list(set(a).union(set(b)))
3. 获取两个 list 的差集

print list(set(b).difference(set(a))) # b中有而a中没有的

2.python Set交集、并集、差集

s = set([3,5,9,10,20,40])      #创建一个数值集合

t = set([3,5,9,1,7,29,81])      #创建一个数值集合

a = t | s          # t 和 s的并集 ,等价于t.union(s)

b = t & s          # t 和 s的交集 ,等价于t.intersection(s)

c = t - s          # 求差集(项在t中,但不在s中)  ,等价于t.difference(s)

d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中),等价于t.symmetric_difference(s)

@差集(字典)

(1)

if __name__ == '__main__':
a_list = [{'a' : 1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5}]
b_list = [{'a' : 1}, {'b' : 2}]
ret_list = []
for item in a_list:
if item not in b_list:
ret_list.append(item)
for item in b_list:
if item not in a_list:
ret_list.append(item)
print(ret_list)

(2)

if __name__ == '__main__':
a_list = [{'a' : 1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5}]
b_list = [{'a' : 1}, {'b' : 2}]
ret_list = [item for item in a_list if item not in b_list] + [item for item in b_list if item not in a_list]
print(ret_list)

参考link:http://www.cnblogs.com/DjangoBlog/p/3510385.html

http://www.jb51.net/article/93166.htm

python List交集、并集、差集的更多相关文章

  1. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  2. (java/javascript) list 交集 并集 差集 去重复并集

    java list 交集 并集 差集 去重复并集 package com; import java.util.ArrayList; import java.util.Iterator; import ...

  3. 如何求ArrayList集合的交集 并集 差集 去重复并集

    需要用到List接口中定义的几个方法: addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 ...

  4. spark之交集并集差集拉链

    spark之交集并集差集拉链 def main(args: Array[String]): Unit = { val sparkConf = new SparkConf().setMaster(&qu ...

  5. java 两个list 交集 并集 差集 去重复并集

    前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayLi ...

  6. python 合集set,交集,并集,差集,对称差集别搞混

    有集合 x与y x = {1,2,3,4,5}y = {4,5,6,7,8} x和y的交集为 {4,5} x和y的对称差集{1, 2, 3, 6, 7, 8} x和y的并集{1, 2, 3, 4, 5 ...

  7. python 集合运算交集&并集&差集

    差集>>> #两个列表的差集3 >>> ret3 = list(set(a) ^ set(b)) #两个列表的差集 >>> ret4=list(s ...

  8. python [] 数组 list 交集 并集 差集

    >>> a = [1,2,3] >>> b = [2,4,5] >>> list(set(a).intersection(set(b))) [2] ...

  9. java list 交集 并集 差集 去重复并集

    package com; import java.util.ArrayList;import java.util.Iterator;import java.util.List; public clas ...

  10. js Array 交集 并集 差集 去重

    最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上,网上的实现方式都是2次循环,性能不适合我的需求,1000*1000那循环次数太多了,所以我这里采用对象object来 ...

随机推荐

  1. Codility---Dominator

    Task description A zero-indexed array A consisting of N integers is given. The dominator of array A ...

  2. uni-app中vue组件父子值传递

    一.父组件向子组件传递数据(props) <template> <view class="container" style="background: # ...

  3. Java 泛型学习总结

    前言 Java 5 添加了泛型,提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,可以为以前处理通用对象的类和方法,指定具体的对象类型.听起来有点抽象, ...

  4. mysql的配置和启动命令

    一.mysql配置文件在linux系统下的位置 使用命令查询位置: 1.找到安装位置 which mysql  -> /usr/bin/mysql 2.接下来就可以针对这个目录通过一些命令查看配 ...

  5. Java多线程(三):volatile

    volatile volatile是一种轻量同步机制.请看例子 MyThread25类 public class MyThread25 extends Thread{ private boolean ...

  6. Codeforces Gym101503E:XOR-omania(构造+思维)

    题目链接 题意 给出m个数b,这些数是由n个数a两两异或组成的,问初始的那n个数分别是多少. 思路 存在多组解的情况...原来是个构造题. 考虑这样一种情况:b1 = a1 ^ a2,b2 = a2 ...

  7. Codeforces 782B:The Meeting Place Cannot Be Changed(三分搜索)

    http://codeforces.com/contest/782/problem/B 题意:有n个人,每个人有一个位置和速度,现在要让这n个人都走到同一个位置,问最少需要的时间是多少. 思路:看上去 ...

  8. mysql的数据存储

    # pycharm 连接mysql import pymysql username = input("输入用户名:") pwd = input("输入密码:") ...

  9. Oracle数据库---序列、索引、同义词

    --创建序列create sequence deptno_seqstart with 50increment by 10maxvalue 70cache 3; --为了方便演示,创建了一个和dept表 ...

  10. ElasticStack学习(六):ElasticSearch搜索初探

    一.ElasticSearch搜索介绍 1.ElasticSearch搜索方式主要分为以下两种: 1).URI Search:此种查询主要是使用Http的Get方法,在URL中使用查询参数进行查询: ...