list1 = set([1, 2, 3, 4, 5, 6, 5, 5, 5])list2 = set([11, 2, 36, ‘a‘, 5, 6, 5, 5, 5])list3 = set([1, 2, 3])list4 = set([‘a‘, ‘b‘, ‘c‘]) 求交集print(list1.intersection(list2))print(list1&list2)输出{2, 5, 6} 求并集print(list1.union(list2))print(list1 | list2)输出{1, 2, 3, 4, 5, 6, 36, ‘a‘, 11} 求差集print(list1.difference(list2)) #在list1不在list2print(list1 - list2)输出{1, 3, 4}print(list2.difference(list1)) #在list2不在list1print(list2 - list1)输出{‘a‘, 11, 36} 子集(无运算符)print(list1.issubset(list2))输出Falseprint(list3.issubset(list1))输出True 对称差集:两个集合互相没有的取出来放一起print(list1.symmetric_difference(list2))输出{1, 3, 36, 4, 11, ‘a‘} isdisjoint:Return True if two sets have a null intersection #两个集合没有交集返回true,否则返回falseprint(list3.isdisjoint(list1))输出Falseprint(list1.isdisjoint(list4))输出True
时间: 2024-12-05 03:46:15