Python Set update() 方法
描述
update() 方法用於修改當前集合,可以添加新的元素或集合到當前集合中,如果添加的元素在集合中已存在,則該元素只會出現一次,重複的會忽略。
語法
update() 方法語法:
set.update(set)
參數
- set -- 必需,可以是元素或集合
返回值
無。
實例
合併兩個集合,重複元素只會出現一次:
實例 1
x = {"apple", "banana", "cherry"}
y = {"google", "zaixian", "apple"}
x.update(y)
print(x)
輸出結果為:
{'banana', 'apple', 'google', 'zaixian', 'cherry'}