两个集合set1和set2的对称差是元素集合,它们在集合set1或set2中的一个中但不在两个集合中。
用法:
set1_name.symmetric_difference(set2_name)
参数:
它只需要一个集合作为参数。如果传递了列表,元组或字典,则将其转换为集合并执行任务。
返回值:
Returns a set which is the symmetric difference between the two sets.
symmetric_difference()的工作代码:
# Python program to demonstrate the use of
# of symmetric_difference() method
list1 = [1, 2, 3]
list2 = [2, 3, 4]
list3 = [3, 4, 5]
# Convert list to sets
set1 = set(list1)
set2 = set(list2)
# Prints the symmetric difference when
# set is passed as a parameter
print(set1.symmetric_difference(set2))
# Prints the symmetric difference when list is
# passed as a parameter by converting it to a set
print(set2.symmetric_difference(list3))
输出:
{1, 4} {2, 5}
注:本文由纯净天空筛选整理自Striver大神的英文原创作品 Python Set symmetric_difference()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。