码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-09-26 21:32:58      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:remove   super   类型   区别   集合   清空   函数   复制   sub   

集合(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 )

标签:remove   super   类型   区别   集合   清空   函数   复制   sub   

原文地址:https://www.cnblogs.com/chen-sang-python/p/9709621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!