认识集合
由一个或多个确定的元素所构成的整体叫做集合。
集合中的元素有三个特征:
1.确定性(集合中的元素必须是确定的)
2.互异性(集合中的元素互不相同。例如:集合A={1,a},则a不能等于1)
3.无序性(集合中的元素没有先后之分),如集合{3,4,5}和{3,5,4}算作同一个集合。
*集合概念存在的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中某个值
集合的定义
s = {1,2,3,1}
#定义可变集合 >>> set_test=set('hello') >>> set_test {'l', 'o', 'e', 'h'} #改为不可变集合frozenset >>> f_set_test=frozenset(set_test) >>> f_set_test frozenset({'l', 'e', 'h', 'o'})
集合的常用操作及关系运算
元素的增加
单个元素的增加 : add(),add的作用类似列表中的append
对序列的增加 : update(),而update类似extend方法,update方法可以支持同时传入多个参数:
>>> a={1,2} >>> a.update([3,4],[1,2,7]) >>> a {1, 2, 3, 4, 7} >>> a.update("hello") >>> a {1, 2, 3, 4, 7, 'h', 'e', 'l', 'o'} >>> a.add("hello") >>> a {1, 2, 3, 4, 'hello', 7, 'h', 'e', 'l', 'o'}
元素的删除
集合删除单个元素有两种方法:
元素不在原集合中时:
set.discard(x)不会抛出异常
set.remove(x)会抛出KeyError错误
>>> a={1,2,3,4} >>> a.discard(1) >>> a {2, 3, 4} >>> a.discard(1) >>> a {2, 3, 4} >>> a.remove(1) Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 1
pop():由于集合是无序的,pop返回的结果不能确定,且当集合为空时调用pop会抛出KeyError错误,
clear():清空集合
>>> a={3,"a",2.1,1} >>> a.pop() 1 >>> a.pop() 3 >>> a.clear() >>> a set() >>> a.pop() Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 'pop from an empty set'
集合操作
|,|=:合集
a = {1,2,3} b = {2,3,4,5} print(a.union(b)) print(a|b)
&.&=:交集
a = {1,2,3} b = {2,3,4,5} print(a.intersection(b)) print(a&b)
-,-=:差集
a = {1,2,3} b = {2,3,4,5} print(a.difference(b)) print(a-b)
^,^=:对称差集
a = {1,2,3} b = {2,3,4,5} print(a.symmetric_difference(b)) print(a^b)
包含关系
in,not in:判断某元素是否在集合内
==,!=:判断两个集合是否相等
两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:
- set.isdisjoint(s):判断两个集合是不是不相交
- set.issuperset(s):判断集合是不是包含其他集合,等同于a>=b
- set.issubset(s):判断集合是不是被其他集合包含,等同于a<=b
集合的工厂函数
class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set. This has no effect if the element is already present. """ pass<span style="color: #0000ff;">def</span> clear(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Remove all elements from this set. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> copy(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return a shallow copy of a set. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> difference(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1-s2 Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> difference_update(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Remove all elements of another set from this set. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> discard(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 与remove功能相同,删除元素不存在时不会抛出异常 Remove an element from a set if it is a member. If the element is not a member, do nothing. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> intersection(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1&s2 Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> intersection_update(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Update a set with the intersection of itself and another. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> isdisjoint(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return True if two sets have a null intersection. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> issubset(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1<=s2 Report whether another set contains this set. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> issuperset(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1>=s2 Report whether this set contains another set. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> pop(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Remove and return an arbitrary set element. Raises KeyError if the set is empty. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> remove(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> symmetric_difference(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1^s2 Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> symmetric_difference_update(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Update a set with the symmetric difference of itself and another. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> union(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 相当于s1|s2 Return the union of sets as a new set. (i.e. all elements that are in either set.) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> update(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Update a set with the union of itself and others. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__and__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__contains__</span>(self, y): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> x.__contains__(y) <==> y in x. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__eq__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self==value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getattribute__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return getattr(self, name). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ge__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__gt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__iand__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self&=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, seq=()): <span style="color: #008000;">#</span><span style="color: #008000;"> known special case of set.__init__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. # (copied from class doc) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ior__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self|=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__isub__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self-=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__iter__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement iter(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ixor__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self^=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__len__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return len(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__le__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__lt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span><span style="color: #000000;"> @staticmethod </span><span style="color: #008000;">#</span><span style="color: #008000;"> known case of __new__</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__new__</span>(*args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Create and return a new object. See help(type) for accurate signature. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ne__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self!=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__or__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self|value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__rand__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return value&self. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__reduce__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return state information for pickling. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__repr__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return repr(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ror__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return value|self. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__rsub__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return value-self. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__rxor__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return value^self. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__sizeof__</span>(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> S.__sizeof__() -> size of S in memory, in bytes </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__sub__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self-value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__xor__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self^value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #800080;">__hash__</span> = None</pre>
课程回顾:
腾讯课堂第一课——流程控制 :https://www.cnblogs.com/l-hf/p/11528934.html
腾讯课堂第二课——循 环 :https://www.cnblogs.com/l-hf/p/11528937.html
腾讯课堂第三课——数字和字符串 : https://www.cnblogs.com/l-hf/p/11528845.html
腾讯课堂第四课——列 表 : https://www.cnblogs.com/l-hf/p/11528858.html
腾讯课堂第四课——字 典 :https://www.cnblogs.com/l-hf/p/11528867.html
腾讯课堂第五课——元 祖 :https://www.cnblogs.com/l-hf/p/11528864.htmll
更多相关内容,详见:
Python全栈开发入门经典:https://ke.qq.com/course/157698#tuin=839b573b
Python全栈开发进阶实战:https://ke.qq.com/course/158006#tuin=839b573b