python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3,4,5] >>> intersection=[v for v in a if v in b] >>> intersection [1, 2, 3, 4, 5] >>> union=b.extend([v for v in a]) >>>
def diff(listA,listB): #求交集的两种方式 retA = [i for i in listA if i in listB] retB = list(set(listA).intersection(set(listB))) print "retA is: ",retA print "retB is: ",retB #求并集 retC = list(set(listA).union(set(listB))) print "retC1 is
__author__ = 'daitr' #--coding:utf-8-- import datetime #方法一: #a=[2,3,4,5] #b=[2,5,8] #tmp = [val for val in a if val in b] #print tmp ##[2, 5] #方法二 #print list(set(a).intersection(set(b))) #print list(set(a).union(set(b))) #print list(set(b).differen
假设我们现在有两个文件 a.txt .b.txt a.txt 中的内容如下: a c 1 3 d 4 b.txt 中的内容如下: a b e 2 1 5 # Example 01 计算并集: [root@VM_81_181_centos ~]# sort -u a.txt b.txt 1 2 3 4 5 a b c d e [root@VM_81_181_centos ~]# # Exmaple 02 计算交集: [root@VM_81_181_centos ~]# grep -F -f a.t
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是grep命令.分别介绍如下: comm命令 , Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-