自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

無序的集合:Python中的數(shù)學集合運算

開發(fā) 后端
set是Python中無序的集合,它可以用于計算標準數(shù)學運算,例如交集、并集、差集和對稱差集,Other集合(例如列表、元組和字典)不支持集合操作,Dict視圖對象類似于集合,可以進行集合操作。本文將詳細探討set對象支持的數(shù)學運算。

set是Python中無序的集合,它可以用于計算標準數(shù)學運算,例如交集、并集、差集和對稱差集,Other集合(例如列表、元組和字典)不支持集合操作,Dict視圖對象類似于集合,可以進行集合操作。本文將詳細探討set對象支持的數(shù)學運算。

先來看一下Set對象支持的數(shù)學運算:

  • union()
  • update()
  • intersection()
  • intersection_update()
  • difference()
  • difference_update()
  • symmetric_difference()
  • symmetric_difference_update()
  • isdisjoint()
  • issubset()
  • issuperset()

Set運算操作可以通過兩種方式完成:使用方法或運算符。

‘union()’

返回一個新集合,其中包含該set和other集合中的元素,使用union()或“|“算子。

語法:

  1. union(*others) 
  2. set | other | ... 

 

 


 

示例1:找到兩個集合的并集—A和B

返回一個包含集合A和集合B中的元素的新集合。但元素不會重復,集合中的所有元素都是唯一的。

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8} 
  3. print (A.union(B))#Output:{1, 2, 3, 4, 5, 6, 8} 
  4. print (A|B)#Output:{1, 2, 3, 4, 5, 6, 8} 

示例2:查找兩個以上集合的并集

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8,10} 
  3. C={1,3,5,7,9} 
  4. print (A|B|C)#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 
  5. print (A.union(B,C))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 

union()方法和|之間的區(qū)別:

  • union():接受任何可迭代的參數(shù)
  • |運算符:僅接受set作為參數(shù),否則將引發(fā)TypeError。

示例3:在union()方法中將iterable用作參數(shù)

  1. A={1,2,3,4,5} 
  2. #iterable is given as list 
  3. print (A.union([2,4,6]))#Output:{1, 2, 3, 4, 5, 6} 
  4. #iterable is given as tuple 
  5. print (A.union((2,4,6)))#Output:{1, 2, 3, 4, 5, 6} 
  6. #iterable is given as range object 
  7. print (A.union(range(5,10)))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9} 
  8. #iterable is given as a dictionary 
  9. print (A.union({'a':6,'b':7}))#Output:{1, 2, 3, 4, 5, 'b''a'

示例4:為|提供參數(shù)iterable

  1. A={1,2,3,4,5} 
  2. B=[1,2,3] 
  3. print (A|B) 
  4. #Output:TypeError: unsupported operand type(s) for |: 'set' and 'list' 

‘update()’

更新集合,并從other集合中添加元素,元素不會重復,集合中的所有元素都是唯一的。通過使用update() 或使用|=運算符來執(zhí)行,返回類型為None,將修改原始集本身。

語法:

  1. update(*others) 
  2. set |= other | ... 

示例1:在A和B兩個集合之間調(diào)用update()

通過添加兩個集合中的元素來更新集合A。

  1. #update() 
  2. A={1,2,3,4,5} 
  3. B={4,5,6,7,8} 
  4. print (A.update(B)) #Output: None 
  5. print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}A={1,2,3,4,5} 
  6. B={4,5,6,7,8} 
  7. A|=B 
  8. print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8} 

示例2:在兩個以上集合之間調(diào)用update()

  1. #update() 
  2. A={1,2,3} 
  3. B={3,4,5} 
  4. C={5,6,7} 
  5. print (A.update(B,C)) #Output: None 
  6. print (A) #Output: {1, 2, 3, 4, 5, 6, 7} 
  7. A={1,2,3} 
  8. B={3,4,5} 
  9. C={5,6,7} 
  10. A|=B|C 
  11. print (A) #Output: {1, 2, 3, 4, 5, 6, 7} 

update() 方法和|=運算符之間的區(qū)別:

  • update() :接受任何可迭代的參數(shù)。
  • =運算符:僅接受set作為參數(shù),否則將引發(fā)TypeError。

示例3:在update() 方法中將iterable用作參數(shù)

  1. A={1,2,3} 
  2. #iterable is given as list 
  3. print (A.update([2,3,4]))#Output:None 
  4. print (A)#Output:{1,2,3,4} 
  5. #iterable is given as tuple 
  6. A={1,2,3} 
  7. A.update((2,3,4)) 
  8. print (A)#Output:{1,2,3,4} 
  9. #iterable is given as range object 
  10. A={1,2,3} 
  11. A.update(range(2,5)) 
  12. print (A)#Output:{1,2,3,4} 
  13. #iterable is given as a dictionary 
  14. A={1,2,3} 
  15. A.update({2:'a',3:'b'}) 
  16. print (A) #Output:{1, 2, 3} 

示例4:為|=運算符提供參數(shù)iterable:

  1. #iterable is given as tuple 
  2. A={1,2,3} 
  3. B=(3,4) 
  4. A|=B#Output:TypeError: unsupported operand type(s) for |=: 'set' and 'tuple' 

‘intersection()’

返回一個具有該集合和other集合共同元素的新集合,通過intersection()或使用&運算符來執(zhí)行。

語法:

  1. intersection(*others) 
  2. set & other & ... 

 

 


 

示例1:找到兩個集合的交集—A和B

返回一個新集合,其中包含集合A和集合B中的共同元素。

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8} 
  3. #intersection is performed by intersection() method or & operator 
  4. print (A.intersection(B))#Output:{2,4} 
  5. print (A&B)#Output:{2,4} 

示例2:找到兩個以上的交集

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8,10} 
  3. C={2,4} 
  4. print (A&B&C)#Output:{2,4} 
  5. print (A.intersection(B,C))#Output:{2,4} 

intersection()方法和&運算符之間的區(qū)別:

  • intersection():接受任何可迭代的參數(shù)。
  • &運算符:僅接受set參數(shù),否則將引發(fā)TypeError。

示例3:在intersection()方法中將iterable用作參數(shù)

  1. A={1,2,3,4,5} 
  2. #iterable is given as list 
  3. print (A.intersection([1,4,6]))#Output:{1,4} 
  4. #iterable is given as tuple 
  5. print (A.intersection((2,4,6)))#Output:{2,4} 
  6. #iterable is given as range object 
  7. print (A.intersection(range(5,10)))#Output:{5} 
  8. #iterable is given as a dictionary 
  9. print (A.intersection({1:'a','b':7}))#Output:{1} 

示例4:為&運算符提供參數(shù)iterable

  1. A={1,2,3,4,5} 
  2. B=[1,2,3] 
  3. print (A&B) 
  4. #Output:TypeError: unsupported operand type(s) for &: 'set' and 'list' 

‘intersection_update()’

更新集合,只保留集合和other中共同的元素??梢酝ㄟ^使用 intersection_update()或使用&=運算符來執(zhí)行,返回類型為None,將修改原始集本身。

語法:

  1. intersection_update(*others) 
  2. set &= other & … 

示例1:找到兩個集合A和B之間的 intersection_update()

通過僅保留在兩個集合中找到的元素來更新集合A。

  1. #intersection_update() 
  2. A={1,2,3,4,5} 
  3. B={4,5,6,7,8} 
  4. print (A.intersection_update(B)) #Output: None 
  5. print (A) #Output: {4,5}A={1,2,3,4,5} 
  6. B={4,5,6,7,8} 
  7. A&=B 
  8. print (A) #Output: {4,5} 

‘difference()’

返回一個去除other中元素之后的新集合,通過difference() 或使用-運算符來執(zhí)行。

語法:

  1. difference(*others) 
  2. set - other - ... 

 

5A9ce2dee869952e2e78.png" target="_blank">5A9ce2dee869952e2e78.png" alt="" title="" width="auto" height="auto" border="0">

示例1:找出A和B兩組之間的差異

返回一個新集合,其中包含在集合A而不在集合B中的元素。

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8} 
  3. print (A.difference(B))#Output:{1,3,5} 
  4. print (A-B)#Output:{1,3,5} 

示例2:找出兩個以上集合之間的差異

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8,10} 
  3. C={2,3} 
  4. print (A-B-C)#Output:{1,5} 
  5. print (A.difference(B,C))#Output:{1,5} 

difference()方法和-運算符之間的區(qū)別:

  • difference():接受任何可迭代的參數(shù)
  • -運算符:僅接受set作為參數(shù)。否則將引發(fā)TypeError。

示例3:在difference()方法中將iterable作為參數(shù)

  1. A={1,2,3,4,5} 
  2. #iterable is given as list 
  3. print (A.difference([1,2,3]))#Output:{4,5} 
  4. #iterable is given as tuple 
  5. print (A.difference((1,2,3)))#Output:{4,5} 
  6. #iterable is given as range object 
  7. print (A.difference(range(1,4)))#Output:{4,5} 
  8. #iterable is given as a dictionary 
  9. print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5} 

示例4:為-運算符提供參數(shù)iterable

  1. A={1,2,3,4,5} 
  2. B=[1,2,3] 
  3. print (A-B) 
  4. #Output:TypeError: unsupported operand type(s) for -: 'set' and 'list' 

‘difference_update()’

從other集合中刪除該集合中的元素,通過使用-= 運算符或使用difference_update() 方法來執(zhí)行,返回類型為None,將修改原始集本身。

句法:

  1. difference_update(*others) 
  2. set -= other | ... 

 

示例1:找出A和B兩組之間的差異

返回一個新集合,其中包含在集合A而不在集合B中的元素。

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8} 
  3. print (A.difference(B))#Output:{1,3,5} 
  4. print (A-B)#Output:{1,3,5} 

示例2:找出兩個以上集合之間的差異

  1. A={1,2,3,4,5} 
  2. B={2,4,6,8,10} 
  3. C={2,3} 
  4. print (A-B-C)#Output:{1,5} 
  5. print (A.difference(B,C))#Output:{1,5} 

difference()方法和-運算符之間的區(qū)別:

  • difference():接受任何可迭代的參數(shù)
  • -運算符:僅接受set作為參數(shù)。否則將引發(fā)TypeError。

示例3:在difference()方法中將iterable作為參數(shù)

  1. A={1,2,3,4,5} 
  2. #iterable is given as list 
  3. print (A.difference([1,2,3]))#Output:{4,5} 
  4. #iterable is given as tuple 
  5. print (A.difference((1,2,3)))#Output:{4,5} 
  6. #iterable is given as range object 
  7. print (A.difference(range(1,4)))#Output:{4,5} 
  8. #iterable is given as a dictionary 
  9. print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5} 

示例4:為-運算符提供參數(shù)iterable

  1. A={1,2,3,4,5} 
  2. B=[1,2,3] 
  3. print (A-B) 
  4. #Output:TypeError: unsupported operand type(s) for -: 'set' and 'list' 

‘difference_update()’

從other集合中刪除該集合中的元素,通過使用-= 運算符或使用difference_update() 方法來執(zhí)行,返回類型為None,將修改原始集本身。

句法:

  1. difference_update(*others) 
  2. set -= other | ... 

 

示例1:找到兩個集合A和B之間的difference_update()

通過刪除集合A和集合B中都存在的元素來更新集合A。

  1. A={1,2,3,4,5} 
  2. B={2,4,6} 
  3. #Return type is None. 
  4. print (A.difference_update(B))#Output:None 
  5. #It will update the original set 
  6. print (A) #Output: {1,3,5} 
  7. # difference_update by using -= operator 
  8. A-=(B) 
  9. print (A) #Output: {1,3,5} 

示例2:查找兩個以上集合之間的difference_update

  1. #difference_update() will modify the original set
  2. A={1,2,3} 
  3. B={1} 
  4. C={2} 
  5. #Return type is None. 
  6. print (A.difference_update(B,C))#Output:None 
  7. #It will update the original set 
  8. print (A) #Output: {3} 
  9. # difference_update by using -= operator 
  10. A={1,2,3} 
  11. B={1} 
  12. C={2} 
  13. A-=B|C 
  14. print (A) #Output: {3} 

difference_update()方法與-=運算符的區(qū)別:

  • difference_update():接受任何可迭代的參數(shù)
  • -=運算符:僅接受set參數(shù),否則將引發(fā)TypeError。

示例3:在difference_update()方法中將iterable作為參數(shù)

  1. #iterable is given as list 
  2. A={1,2,3} 
  3. B=[1] 
  4. print (A.difference_update(B))#Output:None 
  5. print (A)#Output:{2,3} 

示例4:為-=運算符提供參數(shù)iterable

  1. A={1,2,3} 
  2. B=[1] 
  3. A-=Bprint (A)#Output: TypeError: unsupported operand type(s) for -=: 'set' and 'list' 

‘symmetric_difference()’

返回一個新集合,該集合中的元素屬于集合或other,但不包含兩個集合共有的元素。通過symmetric_difference()或使用^運算符來執(zhí)行。

語法:

  1. symmetric_difference(other) 
  2. set ^ other 

 

示例1:找到A和B兩組之間的對稱差

返回一個新集合,其中包含來自集合A和集合B的元素,但不包含兩個集合中共同的元素。

  1. A={1,2} 
  2. B={2,3} 
  3. print (A.symmetric_difference(B))#Output:{1,3} 
  4. print (A^B)#Output:{1,3} 

示例2:對稱差集僅適用于2個集合

多個集合不支持symmetric_difference()方法,如果給出兩個以上的集合,則會引發(fā)TypeError。

  1. A={1,2} 
  2. B={2,3,5} 
  3. C={3,4} 
  4. print (A.symmetric_difference(B,C))#Output:TypeError:symmetric_difference() takes exactly one argument (2 given) 

但是我們可以使用^找到多個集合的對稱差集

  1. A={1,2} 
  2. B={2,3,5} 
  3. C={3,4} 
  4. print (A^B^C)#Output:{1,4,5} 

symmetric_difference()方法和^運算符之間的區(qū)別:

  • symmetric_difference():接受任何可迭代的參數(shù),但此方法不允許使用多個集合。
  • ^運算符:它將僅接受set作為參數(shù)。否則,將引發(fā)TypeError。通過使用^運算符,可以找到多個集合之間的對稱差集。

示例3:在symmetric_difference()方法中將iterable作為參數(shù)

  1. #iterable is given as list 
  2. A={1,2,3} 
  3. B=[1] 
  4. print (A.symmetric_difference(B))#Output:{2,3} 
  5. #iterable is given as tuple 
  6. A={1,2,3} 
  7. B=(1,) 
  8. print (A.symmetric_difference(B))#Output:{2,3} 
  9. #iterable is given as range object 
  10. A={1,2,3} 
  11. B=range(2) 
  12. print (A.symmetric_difference(B))#Output:{2,3} 

示例4:為^運算符提供參數(shù)iterable:

  1. A={1,2,3} 
  2. B=[1] 
  3. A^Bprint (A) #Output: TypeError: unsupported operand type(s) for ^: 'set' and'list' 

‘symmetric_difference_update()’

更新集合,保留在兩個集合中均找到的元素并去除兩個集合中的公共元素??梢酝ㄟ^使用symmetric_difference_update()或使用^=運算符來實現(xiàn),返回類型為None,將修改原始集本身。

語法:

  1. symmetric_difference_update(other) 
  2. set ^= other 

示例1:在A和B兩組之間找到symmetric_difference_update()

將通過僅保留能在任一集合中找到,但不在兩個集合中同時出現(xiàn)的元素來更新集合A。

  1. #symmetric_difference_update() 
  2. A={1,2,3,4,5} 
  3. B={4,5,6,7,8} 
  4. print (A.symmetric_difference_update(B)) #Output: None 
  5. print (A) #Output: {1, 2, 3, 6, 7, 8} 
  6. A={1,2,3,4,5} 
  7. B={4,5,6,7,8} 
  8. A^=B 
  9. print (A) #Output: {1, 2, 3, 6, 7, 8} 

‘isdisjoint()’

如果該集合沒有共同元素,則返回True。當且僅當它們的交集為空集時,這時稱集合之間無連接。

語法:

  1. isdisjoint(other) 

 

5A14c6f7f809.png" target="_blank">5A14c6f7f809.png" alt="" title="" width="auto" height="auto" border="0">

示例

  1. #Set A and Set B containing common elements 
  2. A={1,2,3,4,5} 
  3. B={4,5,6,7,8} 
  4. print (A.isdisjoint(B))#Output:False 
  5. #Set A and Set B not containing common elements 
  6. A={1,2} 
  7. B={3,4} 
  8. print (A.isdisjoint(B))#Output:True 

‘issubset()’

測試集合中的每個元素是否都在other元素中。

語法:

  1. issubset(other) 
  2. set <= other 

 

示例:檢查集合A是否為集合B的子集

可以通過issubset()方法或使用≤運算符來完成。

  1. A={1,2,3,4,5} 
  2. B={4,5,6,7,8} 
  3. print (A.issubset(B)) #OutputFalse 
  4. print (A<=B)#OutputFalse 
  5. A={1,2,3} 
  6. B={1,2,3,4,5} 
  7. print (A.issubset(B)) #OutputTrue 
  8. print (A<=B)#OutputFalse 

Proper subset

測試集合是否為other的真子集,即set <= otherand set != other。

句法:

  1. set < other 

示例:檢查集合A是否是B的真子集

如果兩個集合相等,則意味著 A.issubsetset(B) 返回True,但是真子集A

  1. A={1,2,3,4,5} 
  2. B={4,5,6,7,8} 
  3. print (A<B)#OutputFalse 
  4. A={1,2,3,4,5} 
  5. B={1,2,3,4,5} 
  6. print (A<B)#OutputFalse 
  7. A={1,2,3} 
  8. B={1,2,3,4,5} 
  9. print (A<B)#OutputTrue 

‘issuperset()’

測試other中的每一個元素是否在集合中。

語法:

  1. issuperset(other) 
  2. set >= other 

 

示例:檢查集合A是否為B的超集

可以通過issuperset()方法或使用≥運算符來實現(xiàn):

  1. A={1,2,3,4,5} 
  2. B={4,5,6,7,8} 
  3. print (A.issuperset(B)) #OutputFalse 
  4. print (A>=B)#Output:True 
  5. A={1,2,3,4,5} 
  6. B={1,2,3} 
  7. print (A.issuperset(B)) #OutputTrue 
  8. print (A>=B)#Output:True 

Proper superset

測試集合是否是other集合的真超集,即,set >= otherand set != other。

語法:

  1. set > other 

示例:檢查集合A是否為B的真超集。

如果兩個集合相等,則意味著A.issuperset(B)返回True,但是真超集A> B將返回False。

  1. A={1,2,3,4,5} 
  2. B={4,5} 
  3. print (A>B)#OutputTrue 
  4. A={1,2,3,4,5} 
  5. B={1,2,3,4,5} 
  6. print (A>B)#OutputFalse 
  7. A={1,2,3} 
  8. B={1,2,3,4,5} 
  9. print (A>B)#OutputTrue 

 

總結(jié)

Frozenset不支持所有更新方法,frozenset類型不可變且不可哈希創(chuàng)建,一旦創(chuàng)建內(nèi)容無法更改。由于所有更新方法都修改了原始集,所以frozenset不支持它。

我們可以通過兩種方式執(zhí)行數(shù)學集合設(shè)置操作:使用運算符或使用一種方法。其不同之處在于,如果使用方法,將接受iterable作為參數(shù)。但是對于運算符,僅應(yīng)設(shè)置參數(shù)。如果不是,則會引發(fā) TypeError。所有更新方法都會更新原始集,frozenset不支持該更新。除了更新方法外,所有其他方法都返回一個新集合。

責任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2017-03-14 14:38:21

數(shù)據(jù)庫SQL語句集合運算

2010-11-22 12:14:55

MySQL字段

2023-11-07 18:03:00

Python模塊工具

2024-09-12 17:05:13

2024-11-11 07:00:00

Python編程

2024-09-05 10:49:42

2021-03-09 23:12:51

Python集合項目

2012-01-05 10:19:43

JavaScript

2019-07-22 09:59:20

Java框架集合

2011-07-11 11:02:12

JAVA集合框架

2010-03-09 16:56:08

Python數(shù)據(jù)類型

2017-04-11 12:05:07

機器學習python矩陣運算

2012-02-03 11:17:33

HibernateJava

2022-07-20 12:24:38

Python列表集合

2020-12-30 07:26:20

RedisSortedSet內(nèi)存包

2009-06-30 14:01:00

Java集合框架Java數(shù)組排序

2009-05-08 09:46:37

微軟C#集合對象

2025-03-19 07:48:07

C# 13params參數(shù)

2021-08-19 10:30:13

Java集合排序程序開發(fā)

2022-01-07 19:50:14

元素java集合
點贊
收藏

51CTO技術(shù)棧公眾號