python sets — Unordered collections of unique elements
来源:互联网 发布:翟欣欣的来历 知乎 编辑:程序博客网 时间:2024/05/16 14:33
python sets — 独有元素的无序集合
本文内容部分来自官方文档
Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种。
frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有add,remove方法
In [183]: frozen = frozenset('halo the world, hi!')In [184]: frozenOut[184]: frozenset({' ', '!', ',', 'a', 'd', 'e', 'h', 'i', 'l', 'o', 'r', 't', 'w'})In [185]: frozfrozen frozenset In [185]: frozen.add('halo')#######向不可变集合(frozenset)添加元素报错!!!---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-185-f280cad482cf> in <module>()----> 1 frozen.add('halo')AttributeError: 'frozenset' object has no attribute 'add'######!!!不可变集合没有添加元素的属性In [186]:
sets模块提供了构造和处理独有元素的无序集合的类. 常用来进行成员测试、序列元素去重、计算标准的数学运算(交集、并集、差集、交差)
类似其他集合,sets支持x in set, len(set), 以及 for x in set语法.作为无序集合,sets不记录元素的位置或者插入顺序 相应的,sets不支持索引、切片和其他的序列类似行为 。既然是可变的,所以它不存在哈希值。
大多数的集合应用使用Set类来提供除了hash()的集合方法.对于需要哈希方法的高级应用,ImmutableSet类添加了一个hash()方法,但是却省略了改变集合内容的方法。Set和ImmutableSet都来自BaseSet(一个抽象类用来判定集合 an isinstance(obj, BaseSet).
set的常用操作如下(方法的含义看单词就知道了就不作翻译了):
In [180]: setInstance=set(['ele1','ele2','ele3'])In [181]: setInstance.setInstance.add setInstance.intersection setInstance.removesetInstance.clear setInstance.intersection_update setInstance.symmetric_differencesetInstance.copy setInstance.isdisjoint setInstance.symmetric_difference_updatesetInstance.difference setInstance.issubset setInstance.unionsetInstance.difference_update setInstance.issuperset setInstance.updatesetInstance.discard setInstance.pop In [181]: setInstance.
混合集合类型操作
>>> t | sfrozenset(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])>>> t ^ sfrozenset(['c', 'b', 'e', 'k'])>>> t - s frozenset(['k', 'b'])
如果左右两个操作数的类型相同, 既都是可变集合或不可变集合, 则所产生的结果类型是相同的,但如果左右两个操作数的类型不相同(左操作数是 set,右操作数是 frozenset,或相反情况),则所产生的结果类型与左操作数的类型相同。
集合类使用字典来执行,所以,集合元素的要求和字典元素的键的要求一样。也就是说元素拥有 eq() 和 hash()方法。 因此集合不能包含可变元素,但是可以包含不可变的数据类型如元组或者ImmutableSet的实例. 为了方便执行集合的嵌套,内层集合自动会转换为immutable形式,例如:
Set([Set(['dog'])]) 会转化成 Set([ImmutableSet(['dog'])]).
Set和ImmutableSet的实例都提供以下的操作:
以下操作只存在于 Set 而不存在于 ImmutableSet:
示例
>>> from sets import Set>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])>>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])>>> employees = engineers | programmers | managers # 并集>>> engineering_management = engineers & managers # 交集>>> fulltime_management = managers - engineers - programmers # 差集>>> engineers.add('Marvin') # 添加元素>>> print engineers Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])>>> employees.issuperset(engineers) # 超集测试False>>> employees.update(engineers) # 从另外的集合中进行更新>>> employees.issuperset(engineers)True>>> for group in [engineers, programmers, managers, employees]: ... group.discard('Susan') # 无条件删除元素... print group...Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])Set(['Janice', 'Jack', 'Sam'])Set(['Jane', 'Zack', 'Jack'])Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])
- python sets — Unordered collections of unique elements
- Python 参考库 Sets--独立对象的无序集合(Unordered collections of unique elements)
- The best way to get unique elements of a list in Python
- Python实现Non-unique Elements详解
- get an array of arrays with unique elements
- Guava collections -- Sets
- unordered
- Non-unique Elements
- checkio-non unique elements
- LINTCODE——Sliding Window Unique Elements Sum
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- python——sets模块的使用
- Python习题1:None-Unique Elements 习题+前三名答案解析(checkio.org)
- Python数据结构——collections
- python学习——collections
- toj3843 Intersection of sets
- Python collections
- python Collections
- 设计模式-生产者消费者模式
- 关于volatile
- DTO作用
- webpack--概念5--插件
- Android程序设计基础--总结
- python sets — Unordered collections of unique elements
- POJ3304 Segments(计算几何,线段和直线的交点)
- ansible之条件语句
- 根据类名来跳转对应界面
- javascript 原型链详解 prototype和__proto__
- java面试题---数组中只出现一次的数字
- Android Service学习笔记--基础知识
- MyBatis Sql语句中的转义字符
- C++ 之静态成员 初始化必须在定义外