5.4. Sets(集合)
来源:互联网 发布:大道寺知世和知世公主 编辑:程序博客网 时间:2024/06/06 00:11
Python有一个专门处理集合的数据类型。集合没有重复元素,且是无序的。基础的用法是去重。set对象支持数学上的集合操作,像交并补等。
花括号或是set()函数可被用来创建几何。注意:创建空集合只能用set(),而不是{}。用{}会创建一个空的字典,字典这个数据类型我们会在以后介绍。
这是一个简明的演示:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']>>> fruit = set(basket) # create a set without duplicates>>> fruitset(['orange', 'pear', 'apple', 'banana'])>>> 'orange' in fruit # fast membership testingTrue>>> 'crabgrass' in fruitFalse>>> # Demonstrate set operations on unique letters from two words...>>> a = set('abracadabra')>>> b = set('alacazam')>>> a # unique letters in aset(['a', 'r', 'b', 'c', 'd'])>>> a - b # letters in a but not in bset(['r', 'd', 'b'])>>> a | b # letters in either a or bset(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])>>> a & b # letters in both a and bset(['a', 'c'])>>> a ^ b # letters in a or b but not bothset(['r', 'd', 'b', 'm', 'z', 'l'])跟列表解析近似,集合解析同样被支持:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}>>> aset(['r', 'd'])
译者小结:
集合是无序的,不会含重复元素。
0 0
- 5.4. Sets(集合)
- Redis之 集合(Sets)
- Swift-集合(Sets)
- Ruby 集合指南(2):Hashes、Sets 和 Ranges
- Python 参考库 Sets--独立对象的无序集合(Unordered collections of unique elements)
- 四:redis的sets类型相关操作(有序和无序集合)
- 集合(lists, maps, sets)的两种赋值的方法
- 图论中特殊的集合 Sets in graph
- codeforces 468B Two Sets 集合处理
- SCALA 集合框架文档翻译-Sets
- Swift 集合类型 Arrays 、 Sets 、 Dictionarys - 1
- Redis之Sorted-sets 有序集合类型
- 字符集(Character Sets)
- Sets
- 《Microsoft SQL Server 2008 MDX Step by Step》学习笔记五:生成复杂的集合(Sets)(上)
- 《Microsoft SQL Server 2008 MDX Step by Step》学习笔记六:生成复杂的集合(Sets)(下)
- Redis学习手册——有序集合(Sorted Sets数据类型)
- redis 学习手册之无序集合数据类型sets操作命令
- 修改iOS导航栏字体大小颜色self.navigationItem.title的颜色
- mybatis整合Spring的基本步骤
- Python 匿名函数lambda
- LeetCode OJ - Two Sum
- Error:Execution failed for task ':app:compileDebugNdk' 解决方法
- 5.4. Sets(集合)
- (java)Min Stack
- 芝麻拼房:用分享经济解决酒店“一房难求”
- 安全的应用程序开发和应用程序安全防御
- 无线产品的多种无线加密方式及其区别
- 《高性能MySQL》读书笔记--Schema与数据类型优化
- Mat转IplImage
- Windows访问Linux Samba的共享目录提示 登录失败:用户名或密码错误
- 判断每组是否是最后一个row