Python 集合类(set)学习
来源:互联网 发布:想做淘宝兼职 编辑:程序博客网 时间:2024/06/03 07:50
集合类型
数学上, 把set 称做由不同的元素组成的集合,集合(set)的成员通常被称做集合元素(set elements)。Python 把这个概念引入到它的集合类型对象里。集合对象是一组无序排列的可哈希的值。是的,集合成员可以做字典中的键。数学集合转为Python 的集合对象很有效,集合关系测试和union、intersection 等操作符在Python 里也同样如我们所预想地那样工作。
和其他容器类型一样,集合支持用 in 和 not in 操作符检查成员, 由 len() 内建函数得到集合的基数(大小), 用for 循环迭代集合的成员。但是因为集合本身是无序的,你不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。集合(sets)有两种不同的类型,可变集合(set) 和 不可变集合(frozenset)。如你所想,对可变集合(set),你可以添加和删除元素,对 不可变集合(frozenset)则不允许这样做。请注意,可变集合(set)不是可哈希的,因此既不能用做字典的键也不能做其他集合中的元素。不可变集合(frozenset)则正好相反,即,他们有哈希值,能被用做字典的键或是作为集合中的一个成员。
集合操作符和关系符号。【原文来自python核心编程第二版】
class set(object)方法详解
| set() -> new empty set object| set(iterable) -> new set object
| # 集合创建的唯一方法-用工厂方法set()和frozenset()
| Build an unordered collection of unique elements.
|
| Methods defined here:
| add(...)
| Add an element to a set.
| # 添加成员,如果元素已存在,则不会改变
| This has no effect if the element is already present.
>>> a = set("hello")>>> aset(['h', 'e', 'l', 'o'])>>> a.add("h")>>> aset(['h', 'e', 'l', 'o'])>>>| clear(...)
| Remove all elements from this set.
| # 删除所有元素
| copy(...)
| Return a shallow copy of a set.
| # 浅复制 参考我的另一篇:点击打开链接 比工厂方法快
| difference(...)
| Return the difference of two or more sets as a new set.
| # 差补/相对补集(-)即只属于其中一个集合而且不属于另外一个集合。
| (i.e. all elements that are in this set but not the others.)
|
| difference_update(...)
| Remove all elements of another set from this set.
# 差更新 ( –= )对集合s 和t 进行差更新操作s-=t,差更新操作会返回一个集合,该集合中的成员是集合s 去除掉集合t 中元素后剩余的元素。
>>> a = set("hello")>>> b = set("world")>>> a -= b>>> aset(['e', 'h'])
| discard(...)
| Remove an element from a set if it is a member.
| # s.discard(obj) 如果obj是集合s中的元素,从集合s中删除对象obj
| If the element is not a member, do nothing.
|
| intersection(...)
| Return the intersection of two or more sets as a new set.
| # 求交集(&)(and)
| (i.e. elements that are common to all of the sets.)
|
| intersection_update(...)
| Update a set with the intersection of itself and another.
# 保留/交集更新( &= )保留(或交集更新)操作保留与其他集合的共有成员。
>>> a = set("hello")>>> b = set("world")>>> a &= b>>> aset(['l', 'o'])
| isdisjoint(...)
| Return True if two sets have a null intersection.
| #
| issubset(...)
| Report whether another set contains this set.
| # s.issubset(t),如果s是t的子集,则返回True,否则返回False
| issuperset(...)
| Report whether this set contains another set.
| # s.issuperset(t) 如果t 是s 的超集,则返回True,否则返回False
| pop(...)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
| # 删除任意一个元素,并返回这个元素,参数必须是可哈希的。
| remove(...)
| Remove an element from a set; it must be a member.
| # 删除一个已存在元素,参数必须是可哈希的。
| If the element is not a member, raise a KeyError.
|
| symmetric_difference(...)
| Return the symmetric difference of two sets as a new set.
| # 对称差分(^)即异或(XOR),也就是求异去同,保留两个集合中不同的元素,去掉都存在的元素。
| (i.e. all elements that are in exactly one of the sets.)
|
| symmetric_difference_update(...)
| Update a set with the symmetric difference of itself and another.
>>> a = set("hello")>>> b = set("world")>>> a ^= b>>> aset(['e', 'd', 'h', 'r', 'w'])
| union(...)
| Return the union of sets as a new set.
| # 求并集(|)(OR)
| (i.e. all elements that are in either set.)
>>> a = set("hello")>>> b = set("world")>>> c = a|b>>> aset(['h', 'e', 'l', 'o'])>>> bset(['d', 'r', 'o', 'w', 'l'])>>> cset(['e', 'd', 'h', 'l', 'o', 'r', 'w'])>>> c = a.union(b)>>> cset(['e', 'd', 'h', 'l', 'o', 'r', 'w'])>>> c = b.union(a)>>> cset(['e', 'd', 'h', 'l', 'o', 'r', 'w'])>>> c = a&b>>> cset(['l', 'o'])>>> c = a.intersection(b)>>> cset(['l', 'o'])>>> c = b.intersection(a)>>> cset(['l', 'o'])>>> c = a - b>>> cset(['h', 'e'])>>> c = b - a>>> cset(['r', 'd', 'w'])>>> c = a^b>>> cset(['e', 'd', 'h', 'r', 'w'])>>>| update(...)
| Update a set with the union of itself and others.
# (Union) Update ( |= )这个更新方法从已存在的集合中添加(可能多个)成员
>>> a = set("hello")>>> b = set("world")>>> aset(['h', 'e', 'l', 'o'])>>> bset(['d', 'r', 'o', 'w', 'l'])>>> a |= b>>> aset(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
混合集合类型
可变的set()和不可变的frozenset()操作,如果左右两个操作数的类型相同,既都是可变集合或不可变集合, 则所产生的结果类型是相同的,但如果左右两个操作数的类型不相同(左操作数是set,右操作数是frozenset,或相反情况),则所产生的结果类型与左操作数的类型相同。
相关模块
本文由@The_Third_Wave原创。不定期更新,有错误请指正。
Sina微博关注:@The_Third_Wave
如果这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏!如果您一定要转载,请带上后缀和本文地址。
- Python 集合类(set)学习
- Python学习20:集合类型(set)
- python学习笔记(五)集合(set)
- python:集合(set)
- python 集合(Set)
- Python学习笔记--集合set
- Python学习之set()集合
- python学习笔记(17)--集合(set)详解
- Python学习笔记(五)—集合(set)
- 零基础入门学习Python(14):集合set()
- Python学习笔记:集合(set)基本内容
- Python学习笔记:集合(set)有关函数
- Python(五)set 集合
- Python的集合(Set)
- python set集合(数据结构)
- Python学习笔记 - 10.集合(Set)
- Python学习笔记之集合set
- Set集合HashSet学习(Set集合学习一)
- fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 看看30年来职业冷热变迁 当下什么是铁饭碗
- android 蓝牙浅谈
- 一步一步写算法(之合并排序)
- 第五周作业——有向图邻接表表示及反向图构造
- Python 集合类(set)学习
- 如何让VMare Workstation和Hyper-V共存
- 结构体内存分配问题(转)
- 衍生sub_derived公共
- Hibernate基本映射类型
- 一步一步写算法(之堆排序)
- javascript类型
- 一个合格的程序员应该读过哪些书
- RTP打包发送H264(中)