ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[info] set是一个无序且不重复的元素集合 > ![](https://box.kancloud.cn/69828b4ef1b4b6de338e198706796825_800x500.jpg) [TOC] <br> ### 集合的定义 #### 创建集合set ```cmd >>> s1=set() >>> s1 set() >>> s2={'a','b','c',1,2,3} >>> s2 {1, 2, 3, 'b', 'c', 'a'} >>> type(s2) <class 'set'> ``` ### 集合的内置方法 #### add(value): 添加元素到集合中 由于集合是无效且不重复的,所以添加相同元素,是不会出现重复添加的。 ```cmd >>> s1=set() >>> s1 set() >>> s1.add('a') >>> s1 {'a'} >>> s1.add('a') >>> s1 {'a'} >>> ``` #### clear():清除集合中所有元素 ```cmd >>> s1={'a','b','c'} >>> s1.clear() >>> s1 set() ``` #### copy( ):浅拷贝 ```cmd >>> s1={'a','b','c'} >>> s2=s1.copy() >>> s2 {'b', 'c', 'a'} >>> s1.add('d') >>> s2 {'b', 'c', 'a'} >>> s1 {'b', 'c', 'a', 'd'} ``` #### difference(*args, **kwargs): 集合相减,返回新的集合,不影响原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.difference(s2) {'a'} >>> s1 {'b', 'c', 'a'} ``` 作用等同于 `-`运算: ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1-s2 {'a'} >>> s1 {'b', 'c', 'a'} >>> ``` #### difference_update(*args, **kwargs):集合相减,修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.difference_update(s2) >>> s1 {'a'} ``` #### discard(*args, **kwargs):删除set集合中的元素 ```cmd >>> s1={'a','b','c'} >>> s1.discard('b') >>> s1 {'c', 'a'} ``` #### intersection(*args, **kwargs):取集合交集,返回新集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.intersection(s2) {'b', 'c'} >>> s1 {'b', 'c', 'a'} ``` 取交集,等同于与`&`运算 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1&s2 {'b', 'c'} ``` #### intersection_update(*args, **kwargs):取集合交集,修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.intersection_update(s2) >>> s1 {'b', 'c'} ``` #### isdisjoint(*args, **kwargs): 判断集合是否没有交集 有交集,返回False。无交集,则返回True ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s3={'d','e','f'} >>> s1.isdisjoint(s2) False >>> s1.isdisjoint(s3) True ``` #### issubset(*args, **kwargs):判断集合是否是某个集合的子集 ```cmd >>> s1={'a','b','c'} >>> s2={'a','b','c','d'} >>> s1.issubset(s2) True ``` #### pop():随机删除一个元素,返回元素值 ```cmd >>> s1={'a','b','c'} >>> s1.pop() 'b' >>> s1 {'c', 'a'} ``` #### remove(*args, **kwargs):删除元素 ```cmd >>> s1={'a','b','c'} >>> s1.remove('b') >>> s1 {'c', 'a'} ``` #### symmetric_difference(*args, **kwargs): 取差集,不修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.symmetric_difference(s2) {'a', 'd'} >>> s1 {'b', 'c', 'a'} ``` 取差集,等同于`^`运算 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1^s2 {'a', 'd'} ``` #### symmetric_difference_update(*args, **kwargs): 取差集,修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.symmetric_difference_update(s2) >>> s1 {'a', 'd'} ``` #### union(*args, **kwargs):取并集,不修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.union(s2) {'d', 'b', 'c', 'a'} ``` 取并集,等同于或`|`运算 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1|s2 {'d', 'b', 'c', 'a'} ``` #### update(*args, **kwargs),取并集,修改原集合 ```cmd >>> s1={'a','b','c'} >>> s2={'b','c','d'} >>> s1.update(s2) >>> s1 {'d', 'b', 'c', 'a'} ``` <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/2ff0bc02ec938fef8b6dd7b7f16ee11d_258x258.jpg) ***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***