Python 集合方法总结
1、添加一个元素:
add(...)
Addan element to a set.
1
2
3
4
|
>>> a = { 'shaw' , 11 , 22 } >>>a.add( 'sina' ) >>> a { 'sina' , 11 , 22 , 'shaw' } |
2、清空集合
clear(...)
Remove all elements from this set.
1
2
3
4
|
>>> a = { 'shaw' , 11 , 22 } >>>a.clear() >>> a set () |
3、浅copy
copy(...)
Return a shallow copy of a set.
1
2
3
4
|
>>> a = { 'shaw' , 11 , 22 } >>> b = a.copy() >>> print (b) { 11 , 22 , 'shaw' } |
4、返回一个A中存在,B中不存在元素的集合
difference(...)
Return the difference of two or more setsas a new set.
1
2
3
4
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 11 , "sam" } >>>A.difference(B) { 22 , 'shaw' } |
5、从当前元素中删除和B中相同的元素
difference_update(...)
Remove all elements of another set fromthis set.
1
2
3
4
5
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 11 , "sam" } >>>A.difference_update(B) >>> print (A) { 22 , 'shaw' } |
6、删除指定元素,如果不存在不会报错
discard(...)
Remove an element from a set if it is amember.
1
2
3
4
5
6
7
|
>>> A = { 'shaw' , 11 , 22 } >>>A.discard( 11 ) >>> print (A) { 22 , 'shaw' } >>>A.discard( 15 ) >>> print (A) { 22 , 'shaw' } |
7、取交集
intersection(...)
Return the intersection of two sets as anew set.
1
2
3
4
|
>>> a = { 'shaw' , 11 , 22 } >>> b = { 'sam' , 22 } >>>a.intersection(b) { 22 } |
8、取交集,并更新到A中
intersection_update(...)
Update a set with the intersection ofitself and another.
1
2
3
4
5
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 'sam' , 22 } >>>A.intersection_update(B) >>> print (A) { 22 } |
9、判断是否有交集,如果没有交集,返回True,否则返回False
isdisjoint(...)
Return True if two sets have a nullintersection.
1
2
3
4
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 'sam' , 22 } >>>A.isdisjoint(B) False |
10、判断是否是子序列
issubset(...)
Report whether another set contains thisset.
1
2
3
4
5
6
7
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 'sam' , 22 } >>>B.issubset(A) False >>> C = { 'shaw' } >>>C.issubset(A) True |
11、判断是否是父序列
issuperset(...)
Report whether this set contains anotherset.
1
2
3
4
|
>>> A = { 'shaw' , 11 , 22 } >>> B = { 22 } >>>A.issuperset(B) True |
12、移除元素,如果集合为空,会报错
pop(...)
Remove and return an arbitrary setelement.
Raises KeyError if the set is empty.
1
2
3
4
5
|
>>> A = { 'shaw' , 11 , 1 , 98 , 3 , 'abc' , 'Shaw' } >>>A.pop() 1 >>> print (A) { 98 , 3 , 'abc' , 11 , 'Shaw' , 'shaw' } |
13、删除指定元素,元素不存在会报错
remove(...)
Remove an element from a set; it must be amember.
If the element is not a member, raise aKeyError.
1
2
3
4
5
6
7
8
|
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' } >>>A.remove( '22' ) Traceback (mostrecent call last): File "<input>" , line 1 , in <module> KeyError: '22' >>>A.remove( 22 ) >>> print (A) { 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 'shaw' } |
14、取并集
union(...)
Return the union of sets as a new set.
1
2
3
4
|
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' } >>> B = { 11 , 'sam' } >>>A.union(B) { 'sam' , 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 22 , 'shaw' } |
15、对称差集
symmetric_difference(...)
Return the symmetric difference of twosets as a new set.
1
2
3
4
|
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' } >>> B = { 11 , 'sam' } >>>A.symmetric_difference(B) { 'sam' , 1 , 98 , 3 , 'abc' , 'Shaw' , 22 , 'shaw' } |
16、更新
update(...)
Update a set with the union of itself andothers.
1
2
3
4
5
|
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' } >>> B = { 11 , 'sam' } >>>A.update(B) >>> print (A) { 'sam' , 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 22 , 'shaw' } |
Python 集合方法总结的更多相关文章
- Python集合方法整理(Day9)
#作用:去重,关系运算, #定义: 知识点回顾 可变类型是不可hash类型 不可变类型是可hash类型 #定义集合: 集合:可以包含多个元素,用逗号分割, 集合的元素遵循三个原则: 1:每个元素必须是 ...
- Python 集合set添加删除、交集、并集、集合操作符号
在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- [转]python集合set
Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 来源网 ...
- python集合set,frozenset--笔记
<Python3程序开发指南>笔记. python提供了2种内置的集合类型:可变的set类型.固定的frozenset类型. 只有可哈希运算的对象可添加到集合中.可哈希的数据类型:floa ...
- Python魔法方法总结及注意事项
1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...
- Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了
Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: ...
- python 去重方法
待补充:https://www.cnblogs.com/zknublx/p/6042295.html 一.使用集合直接去重 ids = [1,4,3,3,4,2,3,4,5,6,1]ids = lis ...
- #8 Python数学方法
前言 前几节了解了Python的不同数据类型,有小伙伴会问,不同的数据类型之间是否可以相互转换?肯定是可以的,本篇博文主要记录数字类型的转换,其他类型的相互转换会在下几节记录,Here we go! ...
随机推荐
- android setVisibility失效不起作用的问题
原因:不同的布局中有名字相同的控件,所以adapter中填充item的时候,由于控件名字相同没有正确识别你要显示的控件. 解决方法:给其中一个控件改一下名字就行了,超简单.
- Thrift 个人实战--Thrift RPC服务框架日志的优化
前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还 ...
- 腾讯优测-优社区干货精选 | 那些年,我们在Android机型适配上遇到的坑之Camera拍照时快门咔嚓声
文/腾讯优测研发工程师 吴宇焕 优测小优有话说: android机型适配的坑自然是不少,不想掉坑快来优测优社区~ 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想 ...
- python 杨辉三角
前提:端点的数为1. 每个数等于它上方两数之和. 每行数字左右对称,由1开始逐渐变大. 第n行的数字有n项. 第n行数字和为2n-1. 第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同 ...
- webStorm破解说明
在idea里转一个 -> 注册时选择License server,填http://idea.lanyus.com
- 关于Java的软引用及弱引用
概念介绍 1 Reference 描述一个对象的引用.其内部维持一个queue引用,用于跟踪对象的回收情况,当对象被回收时将当前reference引用入队 2 SoftRef ...
- js兼容性记录
做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...
- 从jsTree演示代码中提取的在线文件查看
从jsTree演示代码中提取的在线文件查看 jsTree 请参考:https://www.jstree.com/ 效果如下: 代码下载:http://files.cnblogs.com/files/z ...
- phpnow修改默认站点根目录的方法
本文转载自:http://blog.csdn.net/andy_eeipla/article/details/7832082 对于phpnow,经测试,修改Apache-20\conf\extra\h ...
- 学习SQL的点点滴滴(三)-修改数据库的兼容级别
语法 ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 } 参数 database_name 要修改的数据库 ...