两组的对称差是元素集合,它们在两个集合中的任何一个中,但不在两个集合中。
对称差异标记为绿色
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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。