set() # 空集合
添加元素
add()
删除
discard()
交集
s1 = {"刘能", "赵四", "⽪⻓⼭"} s2 = {"刘科⻓", "冯乡⻓", "⽪⻓⼭"} s4 = s1.intersection(s2) # 取两个集合的交集 print(s4) # {"⽪⻓⼭"} s3 = s1 & s2 # 取两个集合的交集 print(s3)
并集
s1 = {"刘能", "赵四", "⽪⻓⼭"} s2 = {"刘科⻓", "冯乡⻓", "⽪⻓⼭"} s4 = s1.union(s2) # 取两个集合的并集 {"刘能", "赵四", "⽪⻓⼭","刘科⻓", "冯乡⻓", } print(s4) s3 = s1 | s2 # 取两个集合的并集 print(s3)
差集
s1 = {"刘能", "赵四", "⽪⻓⼭"} s2 = {"刘科⻓", "冯乡⻓", "⽪⻓⼭"} s4 = s1.difference(s2) # 差集,s1中有且s2中没有的值 {"刘能", "赵四"} s6 = s2.difference(s1) # 差集,s2中有且s1中没有的值 {"刘科⻓", "冯乡⻓"} s3 = s1 - s2 # 差集,s1中有且s2中没有的值 s5 = s2 - s1 # 差集,s2中有且s1中没有的值 print(s5,s6)
减,计算差集
&,计算交集
|,计算并集
长度
for循环