###############################集合的关系运算###########################
## #####求两个集合的交集#############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