# #!/user/bin/env python# # -*- coding:utf-8 -*-# python = ['ww', 'xx', 'yy']# linux = ['ww', 'xx', 'xz', 'zz', 'zd']# python_and_linux = []# for i in python:# if i in linux:# python_and_linux.append(i)# print(python_and_linux)## # 求交集# p_n = set(python)# l_n = set(linux)# print(p_n.intersection(l_n))# print(p_n & l_n)## python = ['ww', 'xx', 'yy']# linux = ['ww', 'xx', 'xz', 'zz', 'zd']# p_n = set(python)# l_n = set(linux)# l_n.difference_update(p_n)# print(l_n)## python = ['ww', 'xx', 'yy']# linux = ['ww', 'xx', 'xz', 'zz', 'zd']# p_n = set(python)# l_n = set(linux)# p_n.difference_update(l_n)# print(p_n)## python = ['ww', 'xx', 'yy']# linux = ['ww', 'xx', 'xz', 'zz', 'zd']# p_n = set(python)# l_n = set(linux)## # 求并集、# print(p_n.union(l_n))# print(p_n | l_n)## # 求差集# print(p_n.difference(l_n))# print(l_n - p_n)# print(p_n - l_n)## # 交叉补集# print(p_n ^ l_n)# print(p_n.symmetric_difference(l_n))# # <<<{'xz', 'zz', 'zd', 'yy'}# isdisjoint如果有交集返回false否则返回trues1 = {1, 2}s2 = {1, 2, 3}print(s1.issubset(s2)) # s1是 s2的子集print(s2.issubset(s1))print(s2.issuperset(s1))print(s1.issuperset(s2))s1 = {1, 2}s2 = {1, 2, 3, 4, 5, 96}s1.update([1, 2, 3, 4, 982, 'alex']) # 更新多个值print(s1)# s1.add(1)#更新一个值s1.union(s2)print(s1)s1 = {1, 2}s2 = {1, 2, 3, 4, 5, 96}s = frozenset('hello') # 定义一个不可变集合print(s)# 去除重复names = ['alex', 'alex', 'php']s = set(names)print(s)print(names,'1')# 不能去除重复print(s)s = list(names)print(s)names = ['alex', 'alex', 'php']s = list(set(names))print(s)
来源:https://www.cnblogs.com/pythonzhao/p/11784972.html