set(集合)
- 直接创建一个空集合
set_empty = set() print(set_empty) # set()
- 根据参数创建
# 根据参数 set_argument = set(42,‘345‘,‘ry‘) print(set_argument) # 这样会报错,因为set只允许有一个参数
- 根据列表来创建
set_list = set([11,11,‘45‘,‘11‘,‘ee‘]) print(set_list) # {11, ‘ee‘, ‘45‘, ‘11‘} 会自动删除重复对象
- 根据元组来创建
set_tuple = set((11,11,‘45‘,‘11‘,‘ee‘,)) print(set_tuple) # {‘ee‘, 11, ‘45‘, ‘11‘} 同样会自动删除重复对象
- 根据字典来创建
set_dict = set({‘k1‘:‘v1‘,‘k2‘:2,‘k3‘:‘v3‘,‘k1‘:‘v4‘}) print(set_dict) # {‘k3‘, ‘k1‘, ‘k2‘} 只会存储key值,且不会重复
- 字符串创建
set_str = set(‘Hello Python‘) print(set_str) # {‘y‘, ‘o‘, ‘n‘, ‘ ‘, ‘P‘, ‘t‘, ‘h‘, ‘H‘, ‘e‘, ‘l‘} 同样会删除字符串中重复的字符
- add(self, *args, **kwargs) 添加一个新的元素到集合中,如果添加的元素在集合中已经存在就不会有任何操作
set_init = set([11,11,‘45‘,‘11‘,‘ee‘]) set_init.add(‘Hello‘) print(set_init) # {‘Hello‘, 11, ‘ee‘, ‘11‘, ‘45‘} set_init_str = set(‘Python‘) set_init_str.add(‘Hello,World‘) print(set_init_str) # {‘Hello,World‘, ‘o‘, ‘P‘, ‘t‘, ‘n‘, ‘y‘, ‘h‘}
- clear(self, *args, **kwargs) 清除集合中元素,可以传参数
set_init_clear = set([11,11,‘45‘,‘11‘,‘ee‘]) set_init_clear.clear() print(set_init_clear) # set()
- copy(self, *args, **kwargs) 浅拷贝,会返回一个新的集合
set_init_copy = set([11,11,‘45‘,‘11‘,‘ee‘]) set_receive_copy = set_init_copy.copy() print(set_receive_copy) # {‘ee‘, 11, ‘45‘, ‘11‘}
- difference(self, *args, **kwargs) 比较两个集合类的不同,返回的是被主动比较的那个集合与比较集合的交集之外的子集(数学上叫啥给忘了)
set_init_dif1 = set([11,22,33,44]) set_init_dif2 = set([33,44,55,66]) set_init_rece1 = set_init_dif1.difference(set_init_dif2) print(set_init_rece1) # {11, 22} set_init_rece2 = set_init_dif2.difference(set_init_dif1) print(set_init_rece2) # {66, 55}
- difference_update(self, *args, **kwargs) 删除两个集合的交际
set_init_upd1 = set([11,22,33,44]) set_init_upd2 = set([33,44,55,66]) set_init_upd1.difference_update(set_init_upd2) print(set_init_upd1) # {22, 11} set_init_upd3 = set([11,22,33,44]) set_init_upd4 = set([33,44,55,66]) set_init_upd4.difference_update(set_init_upd3) print(set_init_upd4) # {66, 55}
- discard(self, *args, **kwargs) 删除集合中存在的元素,如果不存在就什么也不做
set_init_dis = set([11,22,33,44,55]) set_init_dis.discard(11) print(set_init_dis) # {33, 44, 22, 55} 只能删除一个
- intersection(self, *args, **kwargs) 返回两个集合中的交集
set_init_inter1 = set([11,22,33,44]) set_init_inter2 = set([33,44,55,66]) set_rece_inter1 = set_init_inter1.intersection(set_init_inter2) print(set_rece_inter1) # {33, 44}
- intersection_update(self, *args, **kwargs) 取出两个集合的交集,更新一个集合
set_inter_upd1 = set([111,222,333,444]) set_inter_upd2 = set([333,444,55,66]) set_inter_upd1.intersection_update(set_inter_upd2) print(set_inter_upd1) # {444, 333}
- isdisjoint(self, *args, **kwargs) 判断两个集合是否有交集,如果有就返回False,如果没有就返回Ture
set_isdis1 = set([111,222,333,444]) set_isdis2 = set([222,333]) set_receive_isdis1 = set_isdis1.isdisjoint(set_isdis2) print(set_receive_isdis1) # False 说明有交集
- issubset(self, *args, **kwargs) 判断A集合是否是B集合的子集
set_issub1 = set([111,222,333,444]) set_issub2 = set([222,333]) set_receive_issub1 = set_issub1.issubset(set_issub2) print(set_receive_issub1) # False 说明set_issub1不是set_issub2的子集 set_receive_issub2 = set_issub2.issubset(set_issub1) print(set_receive_issub2) # True 说明set_issub2是set_issub1的子集
- issuperset(self, *args, **kwargs) 判断A集合是否是B集合的父类,就是A集合是否包含B集合.结果跟 issubset(self, *args, **kwargs) 相反
- pop(self, *args, **kwargs) 移除元素,同时可以定义一个变量来接收移除的元素,如果集合为空会报错
set_pop = set([111,222,333,444,232]) set_receive_pop = set_pop.pop() print(set_pop) # {444, 333, 222, 111} print(set_receive_pop) # 232
- remove(self, *args, **kwargs) 删除元素
set_remove = set([111,222,333,444,232]) set_remove.remove(111) print(set_remove) # {232, 444, 333, 222}
- symmetric_difference(self, *args, **kwargs) 取出两个差,更新到一个集合中,如果同时删除A,B两集合中相同元素删除.
set_sym1 = set([1,2,3,4,5]) set_sym2 = set([2,3,4,5,6,]) set_receive_sym1 = set_sym1.symmetric_difference(set_sym2) print(set_receive_sym1) # {1, 6} set_sym3 = set([1,2,3,4,5]) set_sym4 = set([6,]) set_receive_sym2 = set_sym3.symmetric_difference(set_sym4) print(set_receive_sym2) # {1, 2, 3, 4, 5, 6}
- symmetric_difference_update(self, *args, **kwargs) 将B集合中有,而A集合中没有的更新到A集合中
set_sym_up1 = set([1,2,3,4,5]) set_sym_up2 = set([2,3,4,5,6,]) set_sym_up1.symmetric_difference_update(set_sym_up2) print(set_sym_up1) # {1, 6} set_sym_up3 = set([1,2,3,4,5]) set_sym_up4 = set([6,]) set_sym_up3.symmetric_difference_update(set_sym_up4) print(set_sym_up3) # {1, 2, 3, 4, 5, 6}
- union(self, *args, **kwargs) 返回A集合和B集合的并集
set_un1= set([11,22,33,44]) set_un2 = set([22,33,44,55]) set_rec_un = set_un1.union(set_un2) print(set_rec_un) # {33, 11, 44, 22, 55}
- update(self, *args, **kwargs) A和B的并集,并返回A
set_un_up1 = set([111,222,333,444]) set_un_up2 = set([222,333,444,555]) set_un_up1.update(set_un_up2) print(set_un_up1) # {555, 333, 111, 444, 222}
时间: 2024-10-06 16:47:55