兩個集合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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。