1 # 集合天生可以去重,集合是无序的 2 s = set() #空集合 3 s.add(1)#增加元素 4 s.remove(1)#删除元素 5 6 nx = ['xiaohei','xiaohong','xiaolan','wangwu'] 7 zdh = ['xiaohei','xiaohong','xiaolan','zhansan','lisi'] 8 9 nx_set = set(nx) 10 zdh_set = set(zdh) 11 print(nx_set.intersection(zdh_set))#取交集 12 print(nx_set & zdh_set)#取交集 13 print(nx_set.union(zdh_set))#并集 14 print(nx_set | zdh_set)#并集 15 print(nx_set.difference(zdh_set))#差集 16 print(nx_set - zdh_set)#差集 17 print(nx_set.symmetric_difference(zdh_set))#对称差集 18 print(nx_set ^ zdh_set)#对称差集