python学习(Day03)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习(Day03)相关的知识,希望对你有一定的参考价值。

set

set集合,是一个无序且不重复的元素集合,集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.  

不重复性

技术分享
1 >>> a = [1,1,2,2,3,3,4,4,]
2 >>> set(a)
3 {1, 2, 3, 4}
mxz

无序性

技术分享
1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 a = [gong,shang,jue,zhi,yu,]
4 b = set(a)
5 print(b)
6 
7 #输出结果
8 {zhi, yu, gong, shang, jue}
mxz

set(object)

1.def difference(self,*args,**kwargs): # difference()用来查找a中存在,而b中不存在的元素

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([gong,jue,yu,wen,wu])
3 c = a.difference(b)
4 print(c)
5 
6 # 输出结果
7 {zhi, shang}
mxz

2.def intersection(self,*args,**kwargs): # instersection()用来查找a,b中共同存在的元素(交集)

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([gong,jue,yu,wen,wu])
3 c = a.intersection(b)
4 print(c)
5 
6 # 输出结果
7 {gong, jue, yu}
mxz

3.def symmetric_difference(self,*args,**kwargs): # symmetric_difference()用来查找对称差集,即a,b中不想交的元素,与instersection()相反

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([gong,jue,yu,wen,wu])
3 c = a.symmetric_difference(b)
4 print(c)
5 
6 #输出结果
7 {shang, wen, zhi, wu}
mxz

4.def discard(self,*args,**kwargs):及 def remove(self,*args,**kwargs): # discard()用来删除指定元素,元素不存在不报错,remove()也是用来删除指定元素,但是元素不存在会报错

技术分享
 1 a = set([gong,shang,jue,zhi,yu])
 2 a.discard(gong)
 3 print(a)
 4 
 5 #输出结果
 6 {jue, yu, shang, zhi}
 7 
 8 a = set([gong,shang,jue,zhi,yu])
 9 a.discard(wu)
10 print(a)
11 
12 #输出结果
13 #为空,没有报错
14 
15 
16 a = set([gong,shang,jue,zhi,yu])
17 a.remove(shang)
18 print(a)
19 
20 #输出结果
21 {gong, zhi, jue, yu}
22 
23 a = set([gong,shang,jue,zhi,yu])
24 a.remove(wu)
25 print(a)
26 
27 #输出结果
28 #报错
29 Traceback (most recent call last):
30   File "C:/Users/Administrator/PycharmProjects/untitled4/mxzheng/a/a1.py", line 9, in <module>
31     a.remove(wu)
32 KeyError: wu
mxz

5.def update(self,*args,**kwargs): # update()用来更新

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([ma,xiao,zheng])
3 a.update(b)
4 print(a)
5 
6 #输出结果
7 {gong, zhi, ma, jue, yu, shang, zheng, xiao}
mxz

6.def union(self,*args,**kwargs): # union()用来查找a,b中所有元素(并集)

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([wen,wu,shen,shang])
3 c = a.union(b)
4 print(c)
5 
6 #输出结果
7 {shen, wu, yu, zhi, wen, jue, gong, shang}
mxz

7.def intersection_update(self,*args,**kwargs): # intersection_update() 查找a,b的交集并更新到a

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([shen,shang])
3 a.intersection_update(b)
4 print(a)
5 
6 #输出结果
7 {shang}
mxz

8.def symmetric_difference_update(self,*args,**kwargs): # symmetric_difference_update() 查找a,b的对称差集,并更新到a

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([shen,shang])
3 a.symmetric_difference_update(b)
4 print(a)
5 
6 #输出结果
7 {zhi, gong, yu, shen, jue}
mxz

9.def issubset(self,*args,**kwargs):和 def issuperset(self,*args,**kwargs): # issubset用来查看b是否为a的子序列,issuperset()用来查看a是否为b的父序列,返回结果True or False

技术分享
 1 a = set([gong,shang,jue,zhi,yu])
 2 b = set([gong,shang])
 3 c = a.issubset(b)
 4 print(c)
 5 
 6 #输出结果
 7 False
 8 ...
 9 a = set([gong,shang,jue,zhi,yu])
10 b = set([gong,shang])
11 c = b.issubset(a)
12 print(c)
13 
14 #输出结果
15 True
16 
17 ...
18 a = set([gong,shang,jue,zhi,yu])
19 b = set([gong,shang])
20 c = a.issuperset(b)
21 print(c)
22 
23 #输出结果
24 Ture
25 
26 a = set([gong,shang,jue,zhi,yu])
27 b = set([gong,shang])
28 c = b.issuperset(a)
29 print(c)
30 
31 #输出结果
32 False
mxz

 10.def add(self,*args,**kwargs): #add() #用来添加元素

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = (1,2,3,4,5)
3 a.add(b)
4 print(a)
5 
6 #输出结果
7 {zhi, (1, 2, 3, 4, 5), gong, jue, shang, yu}
mxz

11.def clear(self,*args,**kwargs): #用来清除集合中所有的元素

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 a.clear()
3 print(a)
4 
5 #输出结果
6 set()
mxz

12.def difference_update(self,*args,**kwargs): # 从a集合中从删除和b的交集

技术分享
1 a = set([gong,shang,jue,zhi,yu])
2 b = set([gong,shang])
3 a.difference_update(b)
4 print(a)
5 
6 #输出结果
7 {yu, zhi, jue}
mxz

 

以上是关于python学习(Day03)的主要内容,如果未能解决你的问题,请参考以下文章

day8 python学习

python学习笔记-Day3

Python学习笔记-Day3-set集合操作

从零开始学Python-day4

python学习笔记 Day3

python学习day5--set函数