'''
Set
集合的特点
集合是一个无序的,不重复的数组组合
1、去重,找出2个列表里重复的项。把一个列表变成集合就自动去重了。
2、关系测试,测试两组数据之前的交集、差集、并集等关系
常用操作:
set(字符串),就把列表转换成集合。注意集合的大括号组成{},像字典,但是不是
'''
print(type(set()))
set1 = {1,2,3,4}
print(type(set1))
#set是一个无序,不能重复的集合容器,所以可以用来过滤重复元素。
set2 = {1,2,3,54,2,3,4,1,5,2,4,1,2,3,2,1}
print(set2)
set3 = {'name','age','zidan'}
print(set3)
print(len(set3))
print(list(set2))
dict1 = dict()
print(type(dict1))
print(len(dict1))
m = [(1,2)]
#dict2 = dict()
d = {}
for k,v in m:
d[k]=v
print(k,v)
print(set())
print(set('vzxcv12'))
set1 = {1,2,3,4,'name','老张'}
set1.add('哈哈哈')#新增
set1.remove('哈哈哈')#移除
print(set1)
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set1|set2)#并集
print(set1&set2)#交集
print(set1-set2)#差集
set1 = {1,2,3,4}
set2 = {2,3,5,6}
m = ['z','c','v']
set1.update(m)#将集合 x 并入原集合s中,x 还可以是列表,元组,字典等,x 可以有多个,用逗号分开
print(set1)
set1.discard(1)#将 x 从集合s中移除
print(set1)
set1 = {1,2,3,4}
print(set1.pop())#随机删除并返回集合s中某个值
print(set1)
set1.clear()#清空
print(set1)
set1 = {1,2,3,4}
print(1 in set1)#set同样支持in操作
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set1.union(set2))#并集
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set1.intersection(set2))#交集
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set1.difference(set2))#差集
#symmetric_difference( x )#x也可以是列表,元组,字典。
set1 = {1,2,3,4}
set2 = {2,3,5,6}
ls = [1,2,8,9]
print(set1.symmetric_difference(set2))
print(set1.symmetric_difference(ls))#对称差集,即只在其中一个集合中出现的
print(set1)
#x.issubset(s) 判断 集合s 是否是 集合x 子集
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set1.issubset(set2))
#s.issuperset(x)判断 集合x 是否是集合s的子集
''
set1 = {1,2,3,4}
set2 = {2,3,5,6}
print(set2.issuperset(set1))
'''
网友评论