集合

#Author:Kevin_hou

list_1 = [1,2,3,4,5,6,7,8,8]    #集合也是无序的
list_1 = set(list_1)
list_2 = set([2,4,6,8,10,12])

print(list_1,type(list_1))
print(list_1,list_2)
#取交集
print( list_1.intersection(list_2))

#并集
print(list_1.union(list_2))
#差集list_1-list_2
print(list_1.difference(list_2))
#list_2-list_1
print(list_2.difference(list_1))
#子集
list_3 =set ([2,4,6])
print(list_1.issubset(list_2))
print(list_3.issubset(list_2))
print(list_1.issubset(list_2))

#对称差集
print(list_1.symmetric_difference(list_2))

print("-----------------------------")

list_4 = set([3,7,9])
print(list_3.isdisjoint(list_4))
print("****************************")
#交集
print(list_1&list_2)
#并集
print(list_1|list_2)
#difference
print(list_1-list_2)
#对称差集
print(list_1^list_2)
#添加1项
list_1.add(888)
list_1.update([222,333,444])
print(list_1)
#删除(任意删)
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())

  

上一篇:Ansible常用模块


下一篇:[LeetCode] 539. Minimum Time Difference