集合:有由不同元素组成的一组无序排列的hash值
特点:①由互不相同的元素组成
②无序的
③其中的元素没hash类型,即不可变类型;所以,列表,字典不能作为集合的一级元素。
集合内元素都是互不相同的,在不考虑顺序的情况下,可以采用集合做简单的去除重复项
###########################不考虑元素顺序的情况向下采用集合,简单去重,############## list1=['miku','miku','rin','ran','ran'] set1=set(list1) list1=list(set1) print(list1) # ['ran', 'miku', 'rin']
创建列表:
# set_of_vocaloid={'miku','rin','ran'}#直接定义 # set1=set(['miku','rin','ran'])#使用set函数传入可迭代对象定义 # print(set_of_vocaloid,set1) # #{'ran', 'rin', 'miku'} {'ran', 'rin', 'miku'}
集合的主要方法:
# ####################向集合中添加新的元素######################## set_of_vocaloid={'miku','rin','ran'} # set_of_vocaloid.add('ruka') # print(set_of_vocaloid) # # {'ruka', 'rin', 'miku', 'ran'} set_of_vocaloid.update(['ruka','meiko']) print(set_of_vocaloid)
####################向集合中添加一个新的元素######################## set_of_vocaloid={'miku','rin','ran'} set_of_vocaloid.add('ruka') print(set_of_vocaloid) # {'ruka', 'rin', 'miku', 'ran'}
###################集合浅拷贝############################# set1={'miku','rin','ran'} set2=set1.copy() print(set2)
# ###################清空列表###################### # set1=set('miku') # print(set1) # set1.clear() # print(set1) # # {'i', 'k', 'm', 'u'} # # set()#####清空后将会输出一个空集合
集合的关系运算
###############################集合的关系运算########################### # # #####求两个集合的交集############ # VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} # VOCALOID2={'miku','rin','ruka','洛天依','乐正绫'} # # intersection=VOCALOID1.intersection(VOCALOID2) # # print('a交b:',intersection) # # # a交b {'miku', 'ruka', 'rin'} # #也可以用符号的形式表示两个集合的交集 # intersection1=VOCALOID1&VOCALOID2#等效于intersection=VOCALOID1.intersection(VOCALOID2) # print(intersection1) # ##########求两个集合并集############## # VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} # VOCALOID2={'miku','rin','ruka','洛天依','乐正绫'} # union1=VOCALOID1.union(VOCALOID2) # print('a并b:',union1) # union2=VOCALOID1|VOCALOID2 # print('a|b:',union2)#等效于union1=VOCALOID1.union(VOCALOID2) # # a并b: {'ruka', 'meiko', 'rin', 'miku', 'kaito', '乐正绫', '洛天依', 'ran'} # # a|b: {'ruka', 'meiko', 'rin', 'miku', 'kaito', '乐正绫', '洛天依', 'ran'} # ###########求两个集合的差集########### # VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} # VOCALOID2={'miku','rin','ruka','洛天依','乐正绫'} # difference1=VOCALOID1.difference(VOCALOID2) # print('集合a减集合b:',difference1) # difference2=VOCALOID2-VOCALOID1 # print('b-a:',difference2)#等效于difference1=VOCALOID2.difference(VOCALOID1) # # # 集合a减集合b: {'ran', 'meiko', 'kaito'} # # b-a: {'洛天依', '乐正绫'} # #a-b与b-a将是不同的值 # # # ##########两个集合的交叉补集############ # VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} # VOCALOID2={'miku','rin','ruka','洛天依','乐正绫'} # symmetric1=VOCALOID1.symmetric_difference(VOCALOID2) # print('a与b的交叉补集:',symmetric1) # symmetric2=VOCALOID1^VOCALOID2#等效于symmetric1=VOCALOID1.symmetric_difference(VOCALOID2) # print('a^b:',symmetric2) # # a与b的交叉补集: {'kaito', '乐正绫', 'meiko', '洛天依', 'ran'} # # a^b: {'kaito', '乐正绫', 'meiko', '洛天依', 'ran'} # ############求交叉补集并更新################ # VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} # VOCALOID2={'miku','rin','ruka','洛天依','乐正绫'} # print(VOCALOID1,VOCALOID2) # VOCALOID1.symmetric_difference_update(VOCALOID2)#相当于VOCALOID=VOCALOID1^VOCALOID2 # print(VOCALOID1) # # # {'meiko', 'ran', 'kaito', 'miku', 'ruka', 'rin'} {'乐正绫', '洛天依', 'miku', 'ruka', 'rin'} # # {'乐正绫', 'meiko', '洛天依', 'ran', 'kaito'} #与之相似的还有,intersection_update()求交集并跟新,difference_update()求差集并更新 #####################判断子父级关系################### VOCALOID1={'miku','rin','ran','ruka','meiko','kaito'} VOCALOID2={'miku','rin','ran'} print(VOCALOID1.isdisjoint(VOCALOID2))#Flase 判断是否有交集 print(VOCALOID1.issubset(VOCALOID2))#Flase a是否属于b 等效于VOCALOID1<=VOCALOID2 print(VOCALOID1.issuperset(VOCALOID2))#Ture b是否属于a 等效于VOCALOID2>=VOCALOID1