首页 > 代码库 > Python3中集合的混合使用

Python3中集合的混合使用

比较简单没什么好说的:

list_1 = [1,2,3,4,6,3,2,5,7,8,2,1]
list_1 = set(list_1)
list_1.add(999)
list_2 = set([2,3,444,5,1])
print(list_1,list_2)

list_3 = [1,3,7]
list_3 = set(list_3)
list_4 = [2,5,6]
list_4 = set(list_4)
print(list_1.intersection(list_2))
print(list_1 & list_2)
print(list_1.union(list_2))
print(list_2 | list_1)
print(list_1.difference(list_2))
print(list_1 - list_2)
print(list_1 ^ list_2)
print(list_1.issubset(list_2))
print(list_3.isdisjoint(list_4))

输出结果如下:

{1, 2, 3, 4, 5, 6, 7, 8, 999} {1, 2, 3, 444, 5}
{1, 2, 3, 5}
{1, 2, 3, 5}
{1, 2, 3, 4, 5, 6, 7, 8, 999, 444}
{1, 2, 3, 4, 5, 6, 7, 8, 999, 444}
{8, 999, 4, 6, 7}
{8, 999, 4, 6, 7}
{999, 4, 6, 7, 8, 444}
False
True

Python3中集合的混合使用