您的位置:首页 > 编程语言 > Python开发

python list 求交集 差集 并集

2017-07-04 17:19 260 查看
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' a test module '

__author__ = 'Zhang Shuai'
a = [1,2,3,4]
b = [3,4,5,6]
'''
求交集
'''
#方法1
c = [i for i in a if i in b]
#方法2
c = list(set(a).intersection(set(b)))
'''
求并集
'''
c = list(set(a).union((set(b))))
'''
求差集
'''
c = [i for i in a if i not in b]+[j for j in b if j not in a]

'''
在a中不在b中
'''
c = list(set(a).difference(set(b)))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: