aset = {1, 2, 3}
aset.update() # 不传参数是可以的,保持原样返回
print(aset) # {1, 2, 3}
aset.update({4, 5}) # 合并集合
print(aset) # {1, 2, 3, 4, 5}
aset.update({6, 7}, {8, 9}) # 一次合并多个集合,之后的类型只要可以合并就可以多个,而且不同类型可以混用
print(aset) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
aset.update({'a': 10}) # 更新字典只取键
print(aset) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 'a'}
bset={'b','c'}
bset.update(['d']) # 用列表更新(元组同理)
print(bset) # {'b', 'c', 'd'}
bset.update('ef1') # 用字符串更新
print(bset) # {'b', 'c', 'd', 'e', 'f', '1'}
bset.update(2) # 但是单独的数字不行,update传入的需要是可迭代对象
print(bset) # TypeError
Python集合的update方法
于 2022-06-27 13:20:26 首次发布