Python集合(set)的操作及方法

版权声明:shu淇 https://blog.csdn.net/qq_33206497/article/details/82717806

 

内置方法

  • add(self, element)

用途:添加一个元素到这个set。

返回值:无。

其中,element表示需要被添加的元素。

例:

>>> test = {"python3"}
>>> test.add("python3")
>>> test
{'python3'}  #输出结果,因为set是不会重复的,所以添加相同元素时,输出结果只有一个"python3"

>>> test.add("python2.7")
>>> test
{'python2.7', 'python3'}  #输出结果,set中的元素是无序的
  • clear(self)

用途:清楚set中所有元素。

返回值:无。

例:

>>> test = {"python3"}
>>> test.add("python3")
>>> test
{'python3'}  #输出结果

>>> test.add("python2.7")
>>> test
{'python2.7', 'python3'}  #输出结果

>>> test.clear()
>>> test
set()  #输出结果
  • copy(self)

用途:对当前set进行拷贝。

返回值:返回当前set的复制版本。

例:

>>> test = {"python3"}
>>> testCopy = test.copy()
>>> testCopy
{'python3'}  #输出结果
  • difference(self, s)

用途:求两个set的差集。

返回值:返回两个或多个set的差集。

其中,s表示求差集的set,其可以为一个或多个set

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 4, 5, 6}
>>> test1.difference(test2)
{1, 3}  #输出结果,输出test1在test2中没有的元素

>>> test2.difference(test1)
{5, 6}  #输出结果,输出test2在test1中没有的元素

>>> test3 = {3, 4, 5}
>>> test1.difference(test2, test3)   #difference中可以多个set
{1}  #输出结果

>>> test1 - test2  #另外一种求差集的方式,采用符号-进行
{1, 3}  #输出结果

>>> test1 - test2 - test3  #可直接多个set一起求差集
{1}  #输出结果
  • difference_update(self, s)

用途:求两个set的差集。(与difference不同的是:该方法会将求出的差集更新为调用该方法的set的值)

返回值:无。

其中,s表示求差集的set,其可以为一个或多个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 4, 5, 6}
>>> test1.difference(test2)
{1, 3}  #输出结果,输出test1在test2中没有的元素
>>> test1
{1, 2, 3, 4}  #输出结果,调用difference方法后,test1的值不会改变

>>> test1.difference_update(test2)
>>> test1
{1, 3}  #输出结果,test1的值已经改变

>>> test1 = {1, 2, 3, 4}
>>> test3 = {3, 4, 5}
>>> test1.difference(test2, test3)   #difference中可以多个set
{1}  #输出结果
>>> test1
{1, 2, 3, 4}  #输出结果,test的值没有改变

>>> test1.difference_update(test2, test3)
>>> test1  
{1}  #输出结果,test1的值已经改变
  • intersection(self, s)

用途:求两个set的交集。

返回值:返回两个set的交集。

其中,s表示求交集的set,只能为一个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test1.intersection(test2)
{2, 3, 4}  #输出结果

>>> test1 & test2  #使用&运算符可求出交集
{2, 3, 4}  #输出结果
  • intersection_update(self, s)

用途:求两个set的交集。(与intersection不同的是:该方法会将求出的差集更新为调用该方法的set的值)

返回值:无。

其中,s表示求交集的set,只能为一个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test1.intersection(test2)
{2, 3, 4}  #输出结果

>>> test1
{1, 2, 3, 4}  #输出结果,不会改变test1的值

>>> test1.intersection_update(test2)
>>> test1
{2, 3, 4}  #输出结果,会更新test1的值
  • union(self, s)

用途:求两个set的并集。

返回值:一个并集后的新set。

其中,s表示求并集的set,可多个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test3 = {5, 6, 7, 8}
>>> test1.union(test2)  #与一个set求并集
{1, 2, 3, 4, 5}  #输出结果

>>> test1.union(test2, test3)  #与多个set求并集
{1, 2, 3, 4, 5, 6, 7, 8}  #输出结果

>>> test1 | test2 | test3  #可使用|运算符进行求并集
{1, 2, 3, 4, 5, 6, 7, 8}
  • symmetric_difference(self, s)

用途:求两个set的补集。

返回值:一个求完补集后的新set。

其中,s表示求补集的set,只能一个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test1.symmetric_difference(test2)  #一次只能与一个set求补集
{1, 5}  #输出结果
  • symmetric_difference_update(self, s)

用途:求两个set的补集。

返回值:无。

其中,s表示求补集的set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test1.symmetric_difference(test2)
{1, 5}  #输出结果

>>> test1
{1, 2, 3, 4}  #test1的结果不会改变

>>> test1.symmetric_difference_update(test2)
>>> test1  #更新test1的值
{1, 5}  #输出结果
  • pop(self)

用途:随机移除set中的元素。

返回值:返回在set中随机pop的元素。

例:

>>> test = {1, 2, 3, 4}
>>> test.pop()
1  #输出结果

>>> test.pop()
2  #输出结果
>>> test.pop()
3  #输出结果
  • remove(self, element)

用途:移除set中指定的元素(如果set中没有指定的元素,会报错,建议使用discard方法)。

返回值:无。

例:

>>> test = {1, 2, 3, 4}
>>> test.remove(2)
>>> test  #test已经更新
{1, 3, 4}  #输出结果

>>> test.remove(5)  #test中没有元素5
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    test.remove(5)
KeyError: 5
  • discard(self, element)

用途:移除set中指定的元素(如果set中没有指定的元素,不会报错,建议移除指定元素时使用该方法)。

返回值:无。

例:

>>> test = {1, 2, 3, 4}
>>> test.discard(3)
>>> test
{1, 2, 4}  #输出结果

>>> test.discard(5)  #test中没有元素5,不会报错
>>> test
{1, 2, 4}  #输出结果
  • isdisjoint(self, s)

用途:判断两个set中是否存在交集。

返回值:如果不存在交集则返回True,存在则返回False。

其中,s表示求交集的set,只能为一个set。

例:

>>> test1 = {1, 2, 3, 4}
>>> test2 = {2, 3, 4, 5}
>>> test3 = {5, 6, 7, 8}
>>> test1.isdisjoint(test2)  #test1和test2中存在交集
False  #输出结果

>>> test1.isdisjoint(test3)  #test1和test3中不存在交集
True  #输出结果
 
>>> test1.isdisjoint(test2, test3)  #不能同时与多个set进行判断
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    test1.isdisjoint(test2, test3)
TypeError: isdisjoint() takes exactly one argument (2 given)
>>> 
  • issubset(self, s)

用途:判断当前set是否是另一个set的子集。

返回值:如果另一个set的子集则返回True,否则返回False。

其中,s表示另一个set。

例:

>>> test1 = {1, 2, 3}
>>> test2 = {1, 2, 3, 4}
>>> test1.issubset(test2)  #test1是test2的子集
True  #输出结果

>>> test2.issubset(test1)  #test1不是test2的子集
False
  • issubset(self, s)

用途:判断当前set是否是另一个set的父集。

返回值:如果另一个set的父集则返回True,否则返回False。

其中,s表示另一个set。

例:

>>> test1 = {1, 2, 3}
>>> test2 = {1, 2, 3, 4}
>>> test2.issuperset(test1)  #test1是test2的子集,所以test2肯定是test1的父集
True  #输出结果

>>> test1.issuperset(test2)  
False  #输出结果
  • update(self, s)

用途:用这个set和另一个set的并集更新这个set。

返回值:无。

其中,s表示另一个set,其中s可为多个set。

例:

>>> test1 = {1, 2, 3}
>>> test2 = {1, 2, 3, 4}
>>> test1.update(test2)
>>> test1
{1, 2, 3, 4}  #输出结果,因为set中的元素是不重复的,因此test1和test2求并集的结果如输出结果

>>> test1 = {1, 2, 3}
>>> test2 = {3, 4, 5, 6}
>>> test3 = {"3", "4"}
>>> test1.update(test2, test3)  #union是可以同时与多个set求并集的,update会将求出的并集更新为调用这个方法的的set,因此也可以同时与多个set
>>> test1
{1, 2, 3, 4, 5, 6, '4', '3'}  #输出结果,其中数字3、4是不会与字符串"3"、"4"重复的

猜你喜欢

转载自blog.csdn.net/qq_33206497/article/details/82717806