集合类型 (python)
来源:互联网 发布:python自动化测试工具 编辑:程序博客网 时间:2024/06/03 13:21
集合类型 (python)
在数学上,set称作由不同的元素组成的集合,集合的成员称作集合元素。集合对象是一组无序排列可哈希值。 集合包含两种类型,可变集合(set)和不可变集合(frozenset)。
1集合操作符
数学表示 python符号 python函数 A ∈ B in A ∉ B not in A = B ==A ≠ B != A是B的严格子集 < A是B的子集 <= issubset() ∩ 交集 & intersection() ∪ 并集 | union()s - t 属于s不属于t - difference() △ 属于s或者t,但不同时 ^ symmetric_difference()
2集合操作
2.1创建
>>> s = set('cheeseshope')>>> sset(['c', 'e', 'h', 'o', 'p', 's'])>>> t = frozenset('bookshop')>>> tfrozenset(['b', 'h', 'k', 'o', 'p', 's'])>>> type(s)<type 'set'>>>> type(t)<type 'frozenset'>>>> len(s)6>>> len(t)6
2.2访问
>>> 'c' in sTrue>>> 'ch' in sFalse>>> 'c' not in tTrue>>> [i for i in s]['c', 'e', 'h', 'o', 'p', 's']
2.3更新集合
>>> s = set('cheeseshope')>>> sset(['c', 'e', 'h', 'o', 'p', 's'])>>> s.add('ph')>>> sset(['c', 'e', 'h', 'o', 'p', 's', 'ph'])>>> s.update('yahu')>>> sset(['a', 'c', 'e', 'h', 'o', 'p', 's', 'u', 'y', 'ph'])>>> s.remove('ph')>>> sset(['a', 'c', 'e', 'h', 'o', 'p', 's', 'u', 'y'])>>> s -= set('cookshop')>>> sset(['a', 'e', 'u', 'y'])
3内建函数
len(s) s的长度s.issubset(t) s是t的子集s.issuperset(t) s是t得超集s.union(t) 返回s和t的并集s.intersection(t) 返回s和t的交集s.difference(t) 返回新集合,是s得成员,但不是t的成员s.symmetric_difference(t) 返回新集合,是s和t的成员,但不是共有的成员s.copy() 浅拷贝s.update(t) 将t放入s中,即s现在包含s和ts.intersection_update(t) s中的成员是s和t共有的元素s.difference_update(t) s中得成员属于s,但不属于ts.symmetric_difference_update(t) s中的成员为那些包含在s和t中,但不共有的元素s.add(obj) 在s中添加obj对象s.remove(obj) 在s中移除obj对象s.discard(obj) 如果obj是s中的元素,删除该对象s.pop() 删除集合s中得任意一个对象,并返回s.clear() 删除s中得所有元素
0 0
- 集合类型 (python)
- Python中的集合类型(笔记)
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python学习20:集合类型(set)
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python 里的集合(set)类型
- Python集合(set)类型的操作
- 编程
- Sonatype Nexus 搭建Maven 私服
- [黑马程序员]tcpdemo3
- Oracle循环语句
- 在自己的电脑上搭建服务器,发布自己的网站(学习之用)
- 集合类型 (python)
- 在ORACLE存储过程中创建临时表
- HDU3247 Resource Archiver(AC自动机+DP)
- 闰年判断
- js中生成json对象生成动态表格
- 杭电 1248 寒冰王座
- Mysql sql查询时 if 的用法
- 三大Java 虚拟机垃圾回收机制的比较(HotSpot, JRockit, IBM JVM)
- 多线程调试方法