目的:求多个集合之前的并集,例如:现有四个集合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)每个集合减去包含自己的任意三个集合的共有成员,最后求其中任意两个集合共有的成员。
具体的代码如下:
# encoding: utf-8
def func(content):
# 使用集合实现, 使用集合真是太方便了
c1 = set(content[0]) # [11, 22, 13, 14]
c2 = set(content[1]) # [11, 32, 23, 14, 35]
c3 = set(content[2]) # [11, 22, 38]
c4 = set(content[3]) # [11, 22, 33, 14, 55, 66]
# all collections have element
all_union_elems = c1 & c2 & c3 & c4
if all_union_elems:
print ('all collections have elems: ', all_union_elems)
# three collections have
c1 = c1 - all_union_elems
c2 = c2 - all_union_elems
c3 = c3 - all_union_elems
c4 = c4 - all_union_elems
c123_union_elems = c1 & c2 & c3
c124_union_elems = c1 & c2 & c4
c134_union_elems = c1 & c3 & c4
c234_union_elems = c2 & c3 & c4
if c123_union_elems:
print ("c123_union_elems ", c123_union_elems)
if c124_union_elems:
print ("c124_union_elems ", c124_union_elems)
if c134_union_elems:
print ("c134_union_elems ", c134_union_elems)
if c234_union_elems:
print ("c234_union_elems ", c234_union_elems)
# two collections have
c1 = c1 - c123_union_elems - c124_union_elems - c134_union_elems
c2 = c2 - c123_union_elems - c124_union_elems - c234_union_elems
c3 = c3 - c123_union_elems - c134_union_elems - c234_union_elems
c4 = c4 - c124_union_elems - c134_union_elems - c234_union_elems
c12_union_have = c1 & c2
c13_union_have = c1 & c3
c14_union_have = c1 & c4
c23_union_have = c2 & c3
c24_union_have = c2 & c4
c34_union_have = c3 & c4
if c12_union_have:
print ("c12_union_have ", c12_union_have)
if c13_union_have:
print ("c13_union_have ", c13_union_have)
if c14_union_have:
print ("c14_union_have ", c14_union_have)
if c23_union_have:
print ("c23_union_have ", c23_union_have)
if c24_union_have:
print ("c24_union_have ", c24_union_have)
if c34_union_have:
print ("c34_union_have ", c34_union_have)
c1 = c1 - c12_union_have - c13_union_have - c14_union_have
c2 = c2 - c12_union_have - c23_union_have - c24_union_have
c3 = c3 - c13_union_have - c23_union_have - c34_union_have
c4 = c4 - c14_union_have - c24_union_have - c34_union_have
if c1:
print ('only c1 have ', c1)
if c2:
print ('only c2 have ', c2)
if c3:
print ('only c3 have ', c3)
if c4:
print ('only c4 have ', c4)
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:153708845
if __name__ == "__main__":
content = [[11, 22, 13, 14], [11, 32, 23, 14, 35], [11, 22, 38], [11, 22, 33, 14, 55, 66]]
func(content)
输出结果如下:
all collections have elems: {11}
c124_union_elems {14}
c134_union_elems {22}
only c1 have {13}
only c2 have {32, 35, 23}
only c3 have {38}
only c4 have {33, 66, 55}
这种实现方法其实效率不高,需要比较集合的次数为:1 + 4 + 6 = 11次,另外代码也很冗余,并不是一种好的实现方式。
还有另外一种效率高的实现方式:
(1)首先,先找出成员数最多的那个集合,这里就是集合C4;
(2)将集合C4中的每个成员依次和其它集合进行比较,看其它集合中是否包含此成员;
(3)若其它集合中包括这个成员,就将这个成员从集合中去除,依次这样比较每个集合;
(4)比较一轮之后,集合C4中剩余的成员就是只有自己的成员。
(5)再在除C4以外剩下的集合中,找出成员数最多的集合,重复上诉操作。依次类推,就可以求出各集合之间的并集了。
上述算法中需要比较的次数只有3 + 2 + 1 = 6次。
标签:并集,Python,elems,c3,集合,c2,c1,c4,union From: https://www.cnblogs.com/python1111/p/18215833