目的:求多個集合之前的並集,例如:現有四個集合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次。