python学习笔记3--set

#int string list tuple dict bool float set

#集合:天生去重

s=set()  #空的集合

s2={'1','2','3'}
ls=[1,2,3,4,5,6,7,1,2,3,4]
s3={'1','2'}
print(set(ls))   #对list去重,直接转化成set


#集合是无序的,所有不能通过下标取值
s2.add('2') #添加值
#取交集
print(s2.intersection(s3))
print(s2 & s3)

#取并集
print(s2.union(s3))
print(s2 | s3)

#取差集
print(s2.difference(s3))
print(s2-s3)
原文地址:https://www.cnblogs.com/SuKiWX/p/8662100.html