list_1=set([4,5,6,7])list_2=set([4,8,9])list_3=set([4,5])list_4=set([6,7])a = (2,3)b = (2) #list_3是list_1的子集则为真:print(list_3.issubset(list_1))#b属于a则为真:print (b in a)#找出列表中元素的个数:print(len(list_2))#:a,b中没有任何相同的元素则为真:print (list_2.isdisjoint(list_3))print (list_4.isdisjoint(list_3))#并集:print(list_1 | list_2)print(list_2.union(list_1))#交集:print(list_1 & list_2)print ( list_1.intersection(list_2))#差集(前者有,后者没有,可用于查找后者没有的元素):print (list_1.difference(list_2))print (list_2.difference(list_1)) #b是a的子集则为真:print(list_1.issuperset(list_2))print(list_1.issuperset(list_3)) #对称差集(找出二者中不同的元素):print (list_1.symmetric_difference(list_2))print (list_1 ^ list_2) #给集合添加元素的2种方法:list_1.add(999)list_1.update([77,88])print (list_1)
时间: 2024-10-12 16:08:47