Python设置 symmetric_difference()
两个集合 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}