python 类型之 set
2016-01-29 21:57
465 查看
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.
sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
例子:
结果:
sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
例子:
print('{} a word she can get what she {} for.'.format('with', 'came')) print('{preposition} a word she can get what she {verb} for.'.format(preposition = 'With', verb = 'came')) print('{0} a worf she can get what she {1} for.'.format('with', 'came')) s1 = set(['lk','jim','tom','jim']) print(s1)#打印会删除重复值 s2 = s1.difference(['xzdz','lk']) print(s2)#删除列表中的元素,源文件不修改 print(s1)#不变 s1.difference_update(['xzdz','lk']) print(s1)#源文件修改了 s4 = s1.pop() print(s4)#被删除的元素 print(s1)#删除元素后 x = set('szxpzm') y = set(['h','z','m','x']) print(x&y)#交集 print(x|y)#并集 print(x-y)#差集 print(x,y)
结果:
with a word she can get what she came for. With a word she can get what she came for. with a worf she can get what she came for. {'tom', 'jim', 'lk'} {'tom', 'jim'} {'tom', 'jim', 'lk'} {'tom', 'jim'} tom {'jim'} {'z', 'x', 'm'} {'x', 'm', 'z', 's', 'p', 'h'} {'s', 'p'} {'z', 'x', 's', 'm', 'p'} {'z', 'x', 'm', 'h'}
相关文章推荐
- python class中的@staticmethod
- python大型项目 个人学习中心
- 【Python】用base64和RC4给数据加密/解密
- Python之socket
- Python爬虫实战(5):模拟登录淘宝并获取所有订单
- Python爬虫实战(3):计算大学本学期绩点
- Python爬虫实战(2):百度贴吧帖子
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门(7):正则表达式
- Python pip easy_install源配置
- Python爬虫入门(6):Cookie的使用
- Python爬虫入门(5):URLError异常处理
- Python爬虫入门(4):Urllib库的高级用法
- Python爬虫入门(3):Urllib库的基本使用
- Python爬虫入门(2):爬虫基础了解
- Python爬虫入门(1):综述
- Python 8.3 ThreadLocal
- 用Python如何处理excel里面的数据
- Python subprocess模块总结