當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python set symmetric_difference_update()用法及代碼示例


兩組的對稱差是元素集合,它們在兩個集合中的任何一個中,但不在兩個集合中。

symmetric-difference
對稱差異標記為綠色

symmetric_difference()方法返回一個新集合,其中包含兩個集合的對稱差。 symmetric_difference_update()方法使用集合的對稱差更新調用symmetric_difference_update()的集合。


用法:
A.symmetric_difference_update(B)
參數:
The symmetric_difference takes a single “set” as a argument.
返回:
This method returns None (which indicates absence of a return value). It only updates the set calling symmetric_difference_update() with the symmetric difference of sets.

代碼1

# Python code to demonstrate working of  
# symmetric_difference_update() 
  
A = {'p', 'a', 'w', 'a', 'n'} 
B = {'r', 'a', 'o', 'n', 'e'} 
  
# result is always none. 
result = A.symmetric_difference_update(B) 
  
print('A = ', A) 
print('B = ', B) 
print('result = ', result)

輸出:

('A = ', set(['e', 'o', 'p', 'r', 'w']))
('B = ', set(['a', 'r', 'e', 'o', 'n']))
('result = ', None)

代碼2

# Python code to demonstrate working of  
# symmetric_difference_update() 
  
A = {'s', 'u', 'n', 'n', 'y'} 
B = {'b', 'u', 'n', 'n', 'y'} 
  
# result is always none. 
result = A.symmetric_difference_update(B) 
  
print('A = ', A) 
print('B = ', B) 
print('result = ', result)

輸出:

('A = ', set(['s', 'b']))
('B = ', set(['y', 'b', 'u', 'n']))
('result = ', None)



注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 Python set symmetric_difference_update()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。