#!/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)))
python list 求交集 差集 并集
最新推荐文章于 2024-05-15 10:41:43 发布