小白的python进阶历程------09.集合(set )

Posted chen-sang-python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小白的python进阶历程------09.集合(set )相关的知识,希望对你有一定的参考价值。

集合(set)

无序性,唯一性,可存放任何数据类型

1).set的格式

set = {"name","age","sex","address","job"}
print(set,type(set))
set1 = set()                                             #定义一个空集合,注意与字典的区别
print(set1)

2).set的运算

&:取交集
|:取并集(保留两个集合独有的元素,在加入它们的交集部分内容)
-:取差集(返回某个集合自己独有的元素,具体返回哪个集合的内容取决于谁在-号的左侧)
^:先取并集,再舍弃它们的交集

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
set3 = set1 & set2
print(set3)
set4 = set1 | set2
print(set4)
set5 = set1 - set2
print(set5)
set6 = set1 ^ set2
print(set6)

3).set中的函数

(1).add():增加一个新的元素到集合中

set = {"name","age","sex","address","job"}
set.add("height")  
print(set)

 

(2).clear():清空当前列表中的元素,保留空列表

set = {"name","age","sex","address","job"}
set.clear
print(set)                                                 #结果为set()

 

(3).copy():复制当前列表

set = {"name","age","sex","address","job"}
set2 = set.copy()
print(set2)

 

(4).difference():取差集(-)

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
print(set1.difference(set2))

 

(5).difference_update():求差集,并赋值给源集合

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
set1.difference_update(set2)
print(set1)

 

(6).discard(): 删除集合中指定的元素,如果该集合内没有该元素也不会报错

set = {"name","age",23,"height","address","job"}
set.discard("name")
print(set)

 

(7).intersection():求交集(&)

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
print(set1.intersection(set2))

 

(8).intersection_update():求交集,并赋值给源集合

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
set1.intersection_update(set2)
print(set1)

 

(9).isdisjoint():判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False

set1 = {"name","age",23,"height"}
set2 = {"sex","address","job"}
print(set1.isdisjoint(set2))

 

(10).issubset():判断原始给定集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False

set1 = {23,"sex"}
set2 = {"sex",23,"address","job"}
print(set1.issubset(set2))

 

(11).issuperset():判断指定集合的所有元素是否都包含在原始给定集合中,如果是则返回 True,否则返回 False

set1 = {"sex",23,"address","job"}
set2 = {23,"sex"}
print(set1.issubset(set2))

 

(12).pop():随机删除一个元素

set = {"name","age",23,"height","address","job"}
print(set.pop())

 

(13).remove():删除集合中指定的元素,如果该集合内没有该元素就报错

set = {"name","age",23,"height","address","job"}
set.remove("name")
print(set)

 

(14).sysmmetric_difference():先取并集,再舍弃它们的交集(^)

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
print(set1.symmetric_difference(set2))

 

(15).sysmmetric_difference_update():求交叉补集并赋值给源集合

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
set1.symmetric_difference_update(set2)
print(set1)

 

(16).update():更新集合,参数为可迭代对象

set = {"name","age",23,"height","address","job"}
set.update(("weight","salary"))
print(set)

 

(17).union():求并集(|)

set1 = {"name","age",23,"height","address","job"}
set2 = {"name","age","sex","address","job"}
print(set1.union(set2))

 




以上是关于小白的python进阶历程------09.集合(set )的主要内容,如果未能解决你的问题,请参考以下文章

小白的python进阶历程------05.占位符

小白的python进阶历程------04.转义字符

小白学 Python 爬虫(18):Requests 进阶操作

软件测试/Python 自动化测试训练营/从小白进阶到高手

零基础Python学习路线,小白的进阶之路!

小白艰苦学习之路(记录学习历程——预备篇)Day1