python入门第十天——set集合
Python3 集合
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
集合(set)是一个无序不重复元素的序列。
可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。
哈希值:集合的每一个元素,一定是可哈希的,即不可变类型。
不可变类型:字符串str
可变类型:列表list
>>> a={'apple',1,"",2,4}
>>> type(a)
<class 'set'>
>>> a
{'', 1, 2, 4, 'apple'}
>>> b={1,2,5,9,4,6,2,1,1}
>>> type(a)
<class 'set'>
>>> b
{1, 2, 4, 5, 6, 9}
>>> c=set('hello','world',2,4,2,6,7,1,2,)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
c=set('hello','world',2,4,2,6,7,1,2,)
TypeError: set expected at most 1 arguments, got 9#设置最多1个参数
>>>
>>> c=set('helloworld2426712')
>>> c
{'d', 'w', 'o', 'e', '2', '6', 'h', '1', '4', '7', 'l', 'r'}
集合创建:
>>> c=set('hello') >>> c {'o', 'h', 'l', 'e'} >>> type(c) <class 'set'> >>> c=set('hello','world','2','4','2','6','7','1','2') Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> c=set('hello','world','2','4','2','6','7','1','2') TypeError: set expected at most 1 arguments, got 9 >>> c=set('helloworld2426712') >>> c {'d', 'w', 'o', 'e', '2', '6', 'h', '1', '4', '7', 'l', 'r'} >>> c=set('hello','world') Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> c=set('hello','world') TypeError: set expected at most 1 arguments, got 2 >>> tule_str=('hello','world','2','4','2','6','7','1','2') >>> c=set(tule_str) >>> c {'hello', '6', '2', '1', 'world', '7', '4'} >>>
集合元素的添加:
>>> tule_str=('hello','world','2','4','2','6','7','1','2') >>> c=set(tule_str) >>> c {'hello', '6', '2', '1', 'world', '7', '4'} >>> c.add('中国') #add 添加一个元素 将元素 '中国' 添加到集合 c 中,如果元素已存在,则不进行任何操作 >>> c {'hello', '6', '2', '1', 'world', '7', '4', '中国'} >>> c.update('zhongguo')#update 添加一个序列 >>> c {'u', 'hello', 'o', '6', '2', 'h', 'n', '1', 'z', 'world', '7', '4', '中国', 'g'} >>>
s.update( x )
x 可以有多个,用逗号分开。
>>> c.update([6,9],['d','将']) >>> c {'u', 'd', '将', 'hello', 'o', '6', '2', 6, 9, 'h', 'n', '1', 'z', 'world', '7', '4', '中国', 'g'} >>>
移除元素
s.remove( x )
将元素 x 添加到集合 s 中移除,如果元素不存在,则会发生错误。
>>> c.remove('6') >>> c {'u', 'd', '将', 'hello', 'o', '2', 6, 9, 'h', 'n', '1', 'z', 'world', '7', '4', '中国', 'g'} >>> c.remove(6) >>> c {'u', 'd', '将', 'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', '中国', 'g'} >>> c.remove('将') >>> c {'u', 'd', 'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', '中国', 'g'} >>> c.romove('苏') Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> c.romove('苏') AttributeError: 'set' object has no attribute 'romove' >>>
s.discard( x )
移除集合中的元素,且如果元素不存在,不会发生错误。格式如下所示:
>>> c.discard('中国') >>> c {'u', 'd', 'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>> c.discard('中国') >>> c {'u', 'd', 'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>>
设置随机删除集合中的一个元素,并且返回删除的元素,语法格式如下:
set_str.pop()
>>> c.pop() 'u' >>> c {'d', 'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>> c.pop() 'd' >>> c {'hello', 'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>> c.pop() 'hello' >>> c {'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>>
计算集合元素个数
len(set_Str)
>>> c {'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>> len(c) 11 >>>
清空集合
c.clear()
>>> c {'o', '2', 9, 'h', 'n', '1', 'z', 'world', '7', '4', 'g'} >>> c.clear() >>> c set() >>>
判断元素是否在集合中存在
元素x in s集合 not in in
>>> c set() >>> 'c' in c False >>> c.add('c') >>> 'c' in c True >>> c {'c'} >>>
set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作:
>>> s1 = set([1, 2, 3]) >>> s2 = set([2, 3, 4]) >>> s1 & s2 {2, 3} >>> s1 | s2 {1, 2, 3, 4} >>>
| 运算符 | 方法 | 描述 | 实例 |
|---|---|---|---|
| & | s1.inersection(b) | 交集运算 | set([1,2,3]) & set([2,3,4]) 返回{2,3} |
| | | s1.union(s2) | 并集运算 | set([1,2,3]) & set([2,3,4]) 返回{1,2,3,4} |
| - | s1.difference(s2) | 差集运算 | set([1,2,3]) - set([2,3,4]) 返回{1} |
| ^ | s1.symmetric_difference(s2) | 反向交集运算,对称差集 | set([1,2,3]) - set([2,3,4]) 返回{1, 4} |
| > | s1.issuperset(s3) | 父集,超集 |
s1 > s3 返回 True |
| < | s3.issubset(s1) | 子集, | s3 < s1 返回 True |
>>> s1 = set([1, 2, 3]) >>> s2 = set([2, 3, 4]) >>> s1 or s2 {1, 2, 3} >>> s2 or s1 {2, 3, 4} >>> s1 and s2 {2, 3, 4} >>> s1 & s2 #交集 {2, 3} >>> s1-s2 #差集 {1} >>> s1+s2 Traceback (most recent call last): File "<pyshell#89>", line 1, in <module> s1+s2 TypeError: unsupported operand type(s) for +: 'set' and 'set' >>> s3=set('abc') >>> s4=set('abcd') >>> s3>s4 False >>> s3<s4 True >>>
>>> s1.union(s2) {1, 2, 3, 4} >>> s1.intersection(s2) {2, 3} >>> s1.difference(s2) {1} >>> s1^s2 {1, 4} >>> s1.symmetric_difference(s2) {1, 4} >>>
>>> s1.issuperset(s2) False >>> s1.issubset(s2) False >>> s3={2} >>> s3.issubset(s1) True >>> s3<s1 True >>> s1.issuperset(s3) True >>> s1>s3 True >>>
关系测试和去重是重点知识点
set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。
不可变对象
str是不变对象,而list是可变对象。
对于可变对象,比如list,对list进行操作,list内部的内容是会变化的,比如:
>>> a = ['c', 'b', 'a'] >>> a.sort() >>> a ['a', 'b', 'c'] >>>
对于不可变对象,比如str,对str进行操作
>>> a='abc' >>> a.replace('a','A') 'Abc' >>> a 'abc' >>>
虽然字符串有个replace()方法,也确实变出了'Abc',但变量a最后仍是'abc'
>>> a = 'abc' >>> b = a.replace('a', 'A') >>> b 'Abc' >>> a 'abc'
当我们调用a.replace('a', 'A')时,实际上调用方法replace是作用在字符串对象'abc'上的,而这个方法虽然名字叫replace,但却没有改变字符串'abc'的内容。相反,replace方法创建了一个新字符串'Abc'并返回,如果我们用变量b指向该新字符串,就容易理解了,变量a仍指向原有的字符串'abc',但变量b却指向新字符串'Abc'了:
>>> a='abc' >>> b=a.replace('a','A') 'Abc' >>> a 'abc' >>> id(a) 31341568 >>> id('abc') 31341568 >>> id('abc'[0]) 31260208 >>> 'abc'[0] 'a' >>> id('abc'[1]) 31258528 >>> id('abc'[2]) 5288096 >>> id('Abc') 48975128 >>> id('Abc'[0]) 32365792 >>> id('Abc'[1]) 31258528 >>> id('Abc'[2]) 5288096 >>> id(b) 48400904 >>> id('Abc') 48975520 >>> id('Abc'[0]) 32365792 >>> id(b[0]) 32365792 >>> id(b[1]) 31258528 >>>

所以,对于不变对象来说,调用对象自身的任意方法,也不会改变该对象自身的内容。相反,这些方法会创建新的对象并返回,这样,就保证了不可变对象本身永远是不可变的。

浙公网安备 33010602011771号