本文转载自微信公众号「尤而小屋」,详解作者尤而小屋。集合t建转载本文请联系尤而小屋公众号。议珍 大家好,详解我是集合t建Peter~ 在前面的几篇Python的文章中,我们介绍了Python的议珍多种不同类型的对象:字符串、列表、详解元组、集合t建字典。议珍它们有各自的详解特点: 如果说元组是列表和字符串的杂合体,那么集合可以看做是列表和字典的杂合体 本文的整体目录结构: 集合set的创建有两种方法: 通过set函数创建,空集合只能用这种方式 通过{ }来创建 set() set 注意:空集合必须使用set函数来创建,因为{ }是用来创建空字典的 使用花括号创建 { 1, 2, 3, 4} set 使用set函数创建 { 6, 7, 8, 9} set { 11, 22, 33, 44} 集合的元素不能重复 集合中的元素是不能重复的;如果有重复的元素,集合会自动去重。这是一种非常高效的去重方式 产生的数据中自动将重复的去掉了 set { a, c, i, j, p, r, s, t, v} 当我们创建集合的时候,需要注意数据类型 上面报错中的关键词:unhashable,亿华云中文是不可哈希的。意思是创建的时候存在不可哈希的数据类型:列表 。我们可以记住: 当我们创建集合的时候,元素必须是可哈希的 首先我们通过dir(set)来查看集合的操作方法: [__and__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __gt__, __hash__, __iand__, __init__, __init_subclass__, __ior__, __isub__, __iter__, __ixor__, __le__, __len__, __lt__, __ne__, __new__, __or__, __rand__, __reduce__, __reduce_ex__, __repr__, __ror__, __rsub__, __rxor__, __setattr__, __sizeof__, __str__, __sub__, __subclasshook__, __xor__, add, clear, copy, difference, difference_update, discard, intersection, intersection_update, isdisjoint, issubset, issuperset, pop, remove, symmetric_difference, symmetric_difference_update, union, update] 集合中添加元素 { 6, 7, 8, 9} { 1, 6, 7, 8, 9} { 1, 2, 6, 7, 8, 9} --------------------------------------------------------------------------- 报错提示:列表是不可哈希,也就是可变类型的。之前我们说过:集合中的元素都是可哈希(不可变类型),所以不能直接添加 (1, 3, 5) { (1, 3, 5), 1, 2, 6, 7, 8, 9} update-更新集合 更新集合中的元素,将两个集合中的站群服务器元素组合在一起 { c, go, html, java, javascript, python} 生成的数据中自动将python去重了 { go, html, javascript, python} update的参数不仅仅是集合,它的参数是不可变数据类型: Help on method_descriptor: update(...) Update a set with the union of itself and others. { e, go, h, html, javascript, l, o, python} { 7, 8, 9, e, go, h, html, javascript, l, o, python} pop-随机删除 随机删除一个元素,并且返回删除的结果。pop不能指定参数,也就是不能指定想要删除的元素 python html --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 s9.pop("python") # 不能带参数 TypeError: pop() takes no arguments (1 given) 删除的元素必须在集合中。如果不存在,则会报错 { c, go, html, java, javascript, python} { c, html, java, javascript, python} --------------------------------------------------------------------------- KeyError Traceback (most recent call last) in ----> 1 s8.remove("go") # 再次删除go就会报错,因为go已经不存在了 KeyError: go 指定删除某个元素,如果元素不存在,也不会报错。 { c, html, java, javascript, python} { c, java, javascript, python} { c, java, javascript, python} 上面的结果表明:如果删除的元素不存在,也不会报错。这个是和remove不一样的地方 删除集合中的全部元素 { c, java, javascript, python} set() False 上面通过set方法创建的集合,我们了解到:集合是可变的,也就是可修改的,或者说不可哈希的。 实际上还有一种方式创建的集合是不可变的:frozenset(),可以理解成冻集合,所以就不能进行修改等操作啦。 frozenset({ h, n, o, p, t, y}) 我们查看下这个冻集合的操作方法 [__and__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __gt__, __hash__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __or__, __rand__, __reduce__, __reduce_ex__, __repr__, __ror__, __rsub__, __rxor__, __setattr__, __sizeof__, __str__, __sub__, __subclasshook__, __xor__, copy, difference, intersection, isdisjoint, issubset, issuperset, symmetric_difference, union] [__and__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __gt__, __hash__, __iand__, __init__, __init_subclass__, __ior__, __isub__, __iter__, __ixor__, __le__, __len__, __lt__, __ne__, __new__, __or__, __rand__, __reduce__, __reduce_ex__, __repr__, __ror__, __rsub__, __rxor__, __setattr__, __sizeof__, __str__, __sub__, __subclasshook__, __xor__, add, clear, copy, difference, difference_update, discard, intersection, intersection_update, isdisjoint, issubset, issuperset, pop, remove, symmetric_difference, symmetric_difference_update, union, update] 通过对比两种集合的操作方法,我们发现:冻集合中明显是少了很多增加、删除等方法,所以冻集合是不可变的 在这个小节中介绍的是集合中的各种运算,比如:交集、并集、补集等 in-元素判断 { 1, 2, 3, 4} True False issubset-子集与issuperset-超集 A如果是B的子集,那么B就是A的超集;也就说A的元素全部在B中 { 1, 2} True True { 7, 8, 9, e, go, h, javascript, l, o} False 求两个集合的交集,使用intersection函数或者& { 1, 2, 3, 4} { 1, 2} { 1, 2} { 1, 2} 使用函数union或者|来表示两个集合的并集,会生成一个新的对象 { 1, 2, 3, 4} { javascript, o, 7, l, go, h, 8, 9, e} { 1, 2, 3, 4, 7, 8, 9, e, go, h, javascript, l, o} { 1, 2, 3, 4, 7, 8, 9, e, go, h, javascript, l, o} 使用函数difference或者减号- { 1, 2, 3, 4} { 1, 2} { 3, 4} { 3, 4} set() 使用函数symmetric_difference或者symmetric_difference_update(原地更新数据) { 1, 3, 4, 5, 7} { 1, 2, 3, 4} { 2, 5, 7} { 1, 2, 3, 4} { 2, 5, 7}Python连载文章
集合创建
空集合
s1 = set() # 空集合 s1 非空集合
{ 1, 2, 3, 4}
特别点
set集合方法
add-添加不可变元素
remove-指定删除
discard-指定删除
clear-清空集合
不变的集合-frozenset()
集合运算
intersection-交集
union-并集
difference-差集、补集
symmetric_difference-对称差集