Python 集合 update() 方法

實例

把集合 y 中的項目插入集合 x:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y) 
print(x)

運行實例

定義和用法

update() 方法通過添加另一個集合中的項目來更新當前集合。

如果兩個集合中都存在一個項目,則更新后的集合中將僅出現一次該項目。

語法

set.update(set)

參數值

參數 描述
set 必需。插入當前集合的集合。