python-集合类型


    集合具有唯一性(集合中的元素各不相同),无序性,确定性(集合中的元素是不可改变的,不能是列表,字典以及集合本身)

     1.add(self, *args, **kwargs),union(self, *args, **kwargs),update(self, *args, **kwargs)

      add方法:向字典中添加一个元素,当字典中存在这个元素时,不做任何操作(只能添加一个元素)

      union方法:传入一个可迭代的参数,union方法将迭代出里面的元素,更新到字典的副本中,不改变原字典(传入一个可迭代对象,添加多个元素,原字典不变)

      update方法:与union方法类似,不同的是update方法修改原字典(传入一个可迭代对象,添加多个元素,修改原字典)

复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set1.add('php')
3 >>> set1
4 {'c', 'c++', 'php', 'python', 'java'}
5 >>> set1.add('php')
6 >>> set1
7 {'c', 'c++', 'php', 'python', 'java'}
复制代码
复制代码
 1 >>> set1 = {'python', 'java', 'c', 'c++'}
 2 >>> set2 = {'python',  'c', 'php'}
 3 >>> set1.union(set2)
 4 {'java', 'c', 'c++', 'php', 'python'}
 5 >>> set1.union(['linz'])
 6 {'c', 'c++', 'python', 'java', 'linz'}
 7 >>> set1.union('linz')
 8 {'c', 'c++', 'l', 'i', 'python', 'java', 'n', 'z'}
 9 #set1不发生改变
10 >>> set1
11 {'java', 'c', 'c++', 'python'}
复制代码
复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set1.update({'java', 'php'})
3 >>> set1
4 {'java', 'c', 'c++', 'php', 'python'}
5 >>> set1.update('123')
6 >>> set1
7 {'java', 'c', 'c++', '1', '3', 'php', '2', 'python'}
复制代码

    2.clear(self, *args, **kwargs)

    清空字典中的所有元素

1 >>> set1 = {'java', 'c', 'c++', '1', '3', 'php', '2', 'python'}
2 >>> set1.clear()
3 >>> set1
4 set()

    3.copy(self, *args, **kwargs)

    与列表中的copy方法类似,返回字典的副本,同样的也是浅复制

复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set1.copy()
3 {'java', 'c', 'c++', 'python'}
4 >>> set2 = set1.copy()
5 >>> set2
6 {'java', 'c', 'c++', 'python'}
复制代码

      4.difference(self, *args, **kwargs) 与 difference_update(self, *args, **kwargs)

    求两个集合的差集。difference方法不修改set1,返回一个差集的字典副本;而difference_update方法直接修改set1,等于差集

                                

1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set2 = {'c++', 'go', 'c', 'php'}
3 >>> set1.difference(set2)
4 {'java', 'python'}
1 #set1发生改变
2 >>> set1.difference_update(set2)
3 >>> set1
4 {'java', 'python'}

    5.discard(self, *args, **kwargs),pop(self, *args, **kwargs),remove(self, *args, **kwargs)

      pop方法随机删除集合中的一个元素,当集合为空的时候程序报错

      discard从集合中移除特定元素。如果元素不是该集合成员,则什么也不做。

      remove从一个集合中移除一个元素,这个元素必须是集合的一个成员。如果元素不是成员,则引发键错误。

复制代码
>>> set1 = {'python', 'java', 'c', 'c++'}
>>> set1.pop()
'java'
#当集合为空集时,程序报错
>>> se1 = {}
>>> se1.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 arguments, got 0
复制代码
复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set1.discard('python')
3 >>> set1
4 {'java', 'c', 'c++'}
5 #删除'python'之后,再次删除,程序不做任何事情
6 >>> set1.discard('python')
7 >>> set1
8 {'java', 'c', 'c++'}
复制代码
复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set1.remove('python')
3 >>> set1
4 {'java', 'c', 'c++'}
5 #删除'python'之后,再次删除,程序报错
6 >>> set1.remove('python')
7 Traceback (most recent call last):
8   File "<stdin>", line 1, in <module>
9 KeyError: 'python'
复制代码

    6.intersection(self, *args, **kwargs) 与 intersection_update(self, *args, **kwargs)

      求set1与set2的交集;intersection方法不修改set1,返回一个交集的字典副本;而intersection_update方法直接修改set1,等于交集

                                 

复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set2 = {'c++', 'go', 'c', 'php'}
3 >>> set1.intersection(set2)
4 {'c', 'c++'}
5 >>> set1
6 {'java', 'c', 'c++', 'python'}
复制代码
复制代码
1 #set1发生改变
2 >>> set1 = {'python', 'java', 'c', 'c++'}
3 >>> set2 = {'c++', 'go', 'c', 'php'}
4 >>> set1.intersection_update(set2)
5 >>> set1
6 {'c', 'c++'}
复制代码

    7.isdisjoint(self, *args, **kwargs)

    判断两个集合的交集是不是空集,如果是空集返回True

复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set2 = {'c++', 'go', 'c', 'php'}
3 >>> set1.isdisjoint(set2)
4 False
5 >>> set2 = {'go', 'php'}
6 >>> set1.isdisjoint(set2)
7 True
复制代码

    8.issubset(self, *args, **kwargs) 与 issuperset(self, *args, **kwargs)

    set1.issubset(set2),判断set1是不是set2 的子集,如果是子集,则返回True

    set1.issuperset(set2),判断set1是不是set2的父集(即set2是不是set1的子集),如果是父集,则返回True

复制代码
1 >>> set2 = {'python', 'java', 'c', 'c++'}
2 >>> set1 = {'c++', 'c'}
3 >>> set1.issubset(set2)
4 True
5 >>> set1 = {'c++', 'c', 'go'}
6 >>> set1.issubset(set2)
7 False
复制代码
复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set2 = {'c++', 'c'}
3 >>> set1.issuperset(set2)
4 True
5 >>> set2 = {'c++', 'c', 'go'}
6 >>> set1.issuperset(set2)
7 False
复制代码

    9.symmetric_difference(self, *args, **kwargs) 与 symmetric_difference_update(self, *args, **kwargs)

     求两个集合交叉补集,symmetric_difference方法不修改set1,返回一个交叉补集的字典副本;而symmetric_difference_update方法直接修改set1,等于交叉补集

         

复制代码
1 >>> set1 = {'python', 'java', 'c', 'c++'}
2 >>> set2 = {'php', 'go', 'c', 'c++'}
3 >>> set1.symmetric_difference(set2)
4 {'php', 'python', 'java', 'go'}
5 >>> set1
6 {'java', 'c', 'c++', 'python'}
7 >>> set1.symmetric_difference_update(set2)
8 >>> set1
9 {'python', 'php', 'java', 'go'}
复制代码

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM