集合及深浅copy 02

集合

求交集

#求交集
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 & set2)
print(set2.intersection(set1))
View Code

求并集

#求并集
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 | set2)
print(set2.union(set1))
View Code

 反交集

#求反交集
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 ^ set2)
print(set2.symmetric_difference(set1))
View Code

 差集

#差集
set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7}
print(set1 - set2)
print(set1.difference(set2))
View Code

子集与超集

set1 = {1,2,3}
set2 = {1,2,3,4,5,6,7}
print(set1 < set2)
print(set2.issubset(set1))
View Code
#超集
set1 = {1,2,3}
set2 = {1,2,3,4,5,6,7}
print(set1 < set2)
print(set2.issuperset(set1))
View Code

去重复

set1 = [1,2,3,4,5,6,76,8,23,1,2,3,4,5]
li = set(set1)
print(li)
set1 = list(li)
print(set1)
View Code

 把集合变为不可变集合

s = frozenset("hksahdasl")
print(s,type(s))
View Code

 

 

 

 

 

 

 

 

posted @ 2019-07-23 22:24  套你大象  阅读(131)  评论(0编辑  收藏  举报