# Python基础-集合基础知识点

## 集合

a=[1,2,3,4,5,5,6,6,6,7]
b=set(a)                              #去掉重复的元素
print(b,type(b))
c=(1,1,2,2,2,3,3,4,4,5)
d=set(c)
print(d,type(d))
e='12342435653'
f=set(e)
print(f,type(f))
g={}
print(g,type(g))


a={1,2,3}
for d in a:
print(d)


sets=set((1,2,3,4,5))
print(sets)


a=set('123')
print(a)
print(a)
a.update('abd')            #添加集合
print(a)
b=a.pop()                 #随机删除集合里的一个元素，并返回删的元素
print(b)
a={1,2,3,4,5,5,6,6}
a.remove(3)
#a.remove(0)                 #删除一个没有的元素时会报错
print(a)
print(b)
print(a)


a={1,2,3,4,5}
b={4,5,6}
d={1,2,3}
c=a.union(b)            #联合
print(a)
print(b)
print(c)
c=a.intersection(b)                 #交集
print(c)
c=a.difference(b)                   #a不同于b
print(c)
c=b.difference(a)                   #b不同于a
print(c)
c=a.symmetric_difference(b)
print(c)
print(a.issuperset(d))             #父集，a包含d，d在a集合里，a是b的父集
print(a.issuperset(b))
print(d.issubset(a))               #子集，a包含d，d在a集合里，b是a的子集
print(a.issubset(d))
print(a.isdisjoint(b))      #判断是不是没有交集，没有的话输出true，有就是false
print(b.isdisjoint(d))
c=a-b
print(c)                       #同c=a.difference(b)
c=a|b
print(c)                       #同c=a.union(b)
c=a&b                          #同c=a.intersection(b)
print(c)
print(3 in a)
print(a==b)
print(a!=b)


a=[i for i in range(1000)]
print(a)


a=[i for i in range(0,1000,2)]
print(a)

a=[i for i in range(1000) if i%2==0]
print(a)


a=[i for i in range(200,501) if i%5==0 and i%7==0]
print(a)


a=[[1,2,3],[4,5,6],[7,8,9]]
b=[a[i][1] for i in range(len(a))]            #输出258
print(b)
c=[a[i][2] for i in range(len(a))]            #输出369
print(c)
d=[a[i][i] for i in range(len(a))]            #输出159
print(d)
e=[a[i][i] for i in range(len(a)-1,-1,-1)]    #输出951
print(e)
f=[a[i][-i+2] for i in range(len(a))]         #输出357    f=a[[i][-i-1] for i in range(len(a))]
print(f)
a=[[3*j+1+i for i in range(3)] for j in range(3)]        #输出[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(a)
a=[[4*j+1+i for i in range(4)] for j in range(4)]        #输出[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
print(a)


a={1:20,3:40,5:60}
b={v:k for k,v in a.items()}          #字符反转
print(b)


a={'a':30,'b':4,'A':20,'B':22,'c':21}
b={k.lower():(a.get(k.lower(),0)+a.get(k.upper(),0)) for k in a.keys()}
print(b)


a='123'
for i in a:
print(i)


a=['123',3,5,6,(3,4,5)]
for i in a:
print(i)


for i in range(2,7,3):
print(i)


a='asdfghhjjkk'
for xb,i in enumerate(a):
print(xb,i)


a={'name':'张三','age':'34','add':'China'}
#输出键
for k in a:             #in a,默认是a.keys
print(k)
#输出键
for k in a.keys():
print(k)
#输出值
for v in a.values():
print(v)
#输出键值对
for k,v in a.items():
print(k,v)


a=[1,1,2,2,2,3,3,3,3,3,4,6,6,7,8]
for i in set(a):
print('%d出现的次数为：'%i,a.count(i))