#集合是无序的list_1 = [1,3,4,5,7,3,6,7,9]list_1 = set(list_1) #集合去重 list_2 = set([2,6,0,66,22,8,4])#交集print(list_1.intersection(list_2)) #并集print(list_1.union(list_2))#差集:保留1有 2无的print(list_1.difference(list_2))#子集:3是1的子集list_3 = set([1,3,7])print(list_3.issubset(list_1))#父集print(list_1.issuperset(list_3))#对称差集print(list_1.symmetric_difference(list_2))print("-------------------------")#无交集返回true,否则felselist_4 = set([5,6,8,3])print(list_3.isdisjoint(list_4)) #求交集print(list_1 & list_2)#求并集print(list_1 | list_2)#差集1有 2无print(list_1 - list_2)#对称差集print(list_1 ^ list_2) #增删改查list_1.add(999) #增list_1.update([888,777,555])print(‘list1:‘,list_1)list_1.remove(888) #删,找不到会报错#print(list_1.pop()) #随机删除 print(‘list1:‘,list_1)list_1.discard(777) #删除,找不到不报错print(‘list1:‘,list_1) #print(list_1,list_2)
原文地址:https://www.cnblogs.com/wzsx/p/8963608.html
时间: 2024-10-22 04:07:00