python 数据结构-集合set
2015-03-05 15:18
811 查看
原文地址:http://docs.pythontab.com/python/python3.4/datastructures.html#tut-tuples
集合是一个无序不重复元素的集。
基本功能包括关系测试和消除重复元素。集合对象还支持 union(联合),intersection(交),difference(差)和 sysmmetric difference(对称差集)等数学运算。
大括号或 set() 函数可以用来创建集合。
注意:想要创建空集合,你必须使用 set() 而不是 {}。后者用于创建空字典。
集合是一个无序不重复元素的集。
基本功能包括关系测试和消除重复元素。集合对象还支持 union(联合),intersection(交),difference(差)和 sysmmetric difference(对称差集)等数学运算。
大括号或 set() 函数可以用来创建集合。
注意:想要创建空集合,你必须使用 set() 而不是 {}。后者用于创建空字典。
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # show that duplicates have been removed {'orange', 'banana', 'pear', 'apple'} >>> 'orange' in basket # fast membership testing True >>> 'crabgrass' in basket False >>> # Demonstrate set operations on unique letters from two words ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a {'a', 'r', 'b', 'c', 'd'} >>> a - b # letters in a but not in b {'r', 'd', 'b'} >>> a | b # letters in either a or b {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # letters in both a and b {'a', 'c'} >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'}
相关文章推荐
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- Python数据结构之集合Set
- python基础知识——内置数据结构(集合)
- Python基础数据结构之set
- Redis-数据结构(字符串集合set)
- 步步为营(十二)常用数据结构(5)STL-Set(集合)
- 集合框架体系结构,list、set操作数据,泛型(一)
- Redis-数据结构(有序字符串集合sorted set)
- python数据结构之集合
- JavaScript数据结构与算法之集合(Set)
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- Python基础数据结构之大循环(for list,set,dict,tuple)
- Redis 数据结构之有序集合(sort set)
- python基本数据类型之集合set
- 7、python数据类型之集合set
- JavaScript数据结构与算法之集合(Set)
- python 数据结构六 之 字典与集合
- 【Python】10、python内置数据结构之集合
- Python基础数据类型-列表(list)和元组(tuple)和集合(set)
- Python核心数据类型——集合(Set)