目的:求多个集合之前的并集,例如:现有四个集合C1 = {11, 22, 13, 14}、C2 = {11, 32, 23, 14, 35}、C3 = {11, 22, 38}、C4 = {11, 22, 33, 14, 55, 66},则它们之间的并集应该为:
C1 & C2 & C3 = {11}、C1 & C2 & C4 = {14}、C1 & C3 & C4 = {22}。如下图所示:
实现方法:Python自带了set数据类型,并且可以实现求集合的并集、交集、差集等,十分好用。按照一般的数学方法实现,实现的步骤如下:
(1)先求4个集合共有的成员;
(2)每个集合减去所有集合的共有成员,在求其中任意3个集合共有的成员;
(3)每个集合减去包含自己的任意三个集合的共有成员,最后求其中任意两个集合共有的成员。
具体的代码如下:
1 # encoding: utf-8 2 3 def func(content): 4 # 使用集合实现, 使用集合真是太方便了 5 6 c1 = set(content[0]) # [11, 22, 13, 14] 7 c2 = set(content[1]) # [11, 32, 23, 14, 35] 8 c3 = set(content[2]) # [11, 22, 38] 9 c4 = set(content[3]) # [11, 22, 33, 14, 55, 66] 10 11 # all collections have element 12 all_union_elems = c1 & c2 & c3 & c4 13 if all_union_elems: 14 print ('all collections have elems: ', all_union_elems) 15 16 # three collections have 17 c1 = c1 - all_union_elems 18 c2 = c2 - all_union_elems 19 c3 = c3 - all_union_elems 20 c4 = c4 - all_union_elems 21 c123_union_elems = c1 & c2 & c3 22 c124_union_elems = c1 & c2 & c4 23 c134_union_elems = c1 & c3 & c4 24 c234_union_elems = c2 & c3 & c4 25 if c123_union_elems: 26 print ("c123_union_elems ", c123_union_elems) 27 if c124_union_elems: 28 print ("c124_union_elems ", c124_union_elems) 29 if c134_union_elems: 30 print ("c134_union_elems ", c134_union_elems) 31 if c234_union_elems: 32 print ("c234_union_elems ", c234_union_elems) 33 34 # two collections have 35 c1 = c1 - c123_union_elems - c124_union_elems - c134_union_elems 36 c2 = c2 - c123_union_elems - c124_union_elems - c234_union_elems 37 c3 = c3 - c123_union_elems - c134_union_elems - c234_union_elems 38 c4 = c4 - c124_union_elems - c134_union_elems - c234_union_elems 39 c12_union_have = c1 & c2 40 c13_union_have = c1 & c3 41 c14_union_have = c1 & c4 42 c23_union_have = c2 & c3 43 c24_union_have = c2 & c4 44 c34_union_have = c3 & c4 45 if c12_union_have: 46 print ("c12_union_have ", c12_union_have) 47 if c13_union_have: 48 print ("c13_union_have ", c13_union_have) 49 if c14_union_have: 50 print ("c14_union_have ", c14_union_have) 51 if c23_union_have: 52 print ("c23_union_have ", c23_union_have) 53 if c24_union_have: 54 print ("c24_union_have ", c24_union_have) 55 if c34_union_have: 56 print ("c34_union_have ", c34_union_have) 57 58 c1 = c1 - c12_union_have - c13_union_have - c14_union_have 59 c2 = c2 - c12_union_have - c23_union_have - c24_union_have 60 c3 = c3 - c13_union_have - c23_union_have - c34_union_have 61 c4 = c4 - c14_union_have - c24_union_have - c34_union_have 62 if c1: 63 print ('only c1 have ', c1) 64 if c2: 65 print ('only c2 have ', c2) 66 if c3: 67 print ('only c3 have ', c3) 68 if c4: 69 print ('only c4 have ', c4) 70 71 72 if __name__ == "__main__": 73 74 content = [[11, 22, 13, 14], [11, 32, 23, 14, 35], [11, 22, 38], [11, 22, 33, 14, 55, 66]] 75 76 func(content)
输出结果如下:
1 all collections have elems: {11} 2 c124_union_elems {14} 3 c134_union_elems {22} 4 only c1 have {13} 5 only c2 have {32, 35, 23} 6 only c3 have {38} 7 only c4 have {33, 66, 55}
这种实现方法其实效率不高,需要比较集合的次数为:1 + 4 + 6 = 11次,另外代码也很冗余,并不是一种好的实现方式。
还有另外一种效率高的实现方式:
(1)首先,先找出成员数最多的那个集合,这里就是集合C4;
(2)将集合C4中的每个成员依次和其它集合进行比较,看其它集合中是否包含此成员;
(3)若其它集合中包括这个成员,就将这个成员从集合中去除,依次这样比较每个集合;
(4)比较一轮之后,集合C4中剩余的成员就是只有自己的成员。
(5)再在除C4以外剩下的集合中,找出成员数最多的集合,重复上诉操作。依次类推,就可以求出各集合之间的并集了。
上述算法中需要比较的次数只有3 + 2 + 1 = 6次。这种思路实现的代码可参考我的另一篇博客:https://www.cnblogs.com/mrlayfolk/p/12383674.html。