标签:
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‘} 同样会删除字符串中重复的字符
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‘}
set_init_clear = set([11,11,‘45‘,‘11‘,‘ee‘]) set_init_clear.clear() print(set_init_clear) # set()
set_init_copy = set([11,11,‘45‘,‘11‘,‘ee‘]) set_receive_copy = set_init_copy.copy() print(set_receive_copy) # {‘ee‘, 11, ‘45‘, ‘11‘}
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}
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}
set_init_dis = set([11,22,33,44,55]) set_init_dis.discard(11) print(set_init_dis) # {33, 44, 22, 55} 只能删除一个
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}
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}
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 说明有交集
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的子集
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
set_remove = set([111,222,333,444,232]) set_remove.remove(111) print(set_remove) # {232, 444, 333, 222}
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}
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}
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}
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}
标签:
原文地址:http://www.cnblogs.com/wang-com/p/5856181.html