Python set解析
来源:互联网 发布:关注滚球用什么软件 编辑:程序博客网 时间:2024/05/20 03:07
在python中,set是一种强大的数据类型
set:集合,是一个无序不重复元素集
1.创建集合
向内置的函数set传递一个列表或可迭代对象
>>> a=set('abcdef')>>> a{'b', 'c', 'a', 'e', 'd', 'f'}>>> b=set('abc')>>> b{'c', 'b', 'a'}#python 3.0还可以s={'a','b','c'}##注意s={} #此时创建的是一个字典
2.查询元素
set没有提供查询单独元素的方法
3.set的运算
>>> a-b #补集{'d', 'f', 'e'}>>> a|b #并集{'b', 'e', 'c', 'd', 'f', 'a'}>>> a&b #交集{'c', 'b', 'a'}
4.改变集合
>>> b.add('d') #增加一个元素>>> b{'d', 'c', 'b', 'a'}>>> b.update(['g','h']) #求并集>>> b{'b', 'h', 'c', 'g', 'a', 'd'}>>> b.remove('g') #删除一个元素>>> b{'b', 'h', 'c', 'a', 'd'}
5.集合解析
>>> {s*2 for s in [1,2,3,4] } #注意是大括号{8, 2, 4, 6}
6.过滤重复元素,可用于数据去重
>>> a=[1,2,3,4,4,5,6,7,7,9]>>> b=set(a)>>> b{1, 2, 3, 4, 5, 6, 7, 9}
阅读全文
0 0
- Python set解析
- 第三章Python解析list,dictionary,set
- 6.python的set、list和dict的解析
- Python Set
- python set
- python set
- python--set
- python set
- python set
- python set
- Python---set
- Python set
- python set
- python set
- python set
- python-set
- Python set
- python set()
- ionic开发——Node Sass does not yet support your current environment解决办法
- 算法导论 练习题 16.1-5
- 使用jquery Ajax请求action 得到返回的数据
- 高考难与领导沟通更难 与领导有效沟通的清单
- <script>标签不能自闭合
- Python set解析
- javascript中的dijit/form/HorizontalSlider地用法
- C#调用c++写的dll不成功原因
- 【Python学习系列一】Windows下Python及其IDE(eclipse+pydev)安装
- Day01-HTML
- ES6(十一)Promise
- 轮式装载机工作原理
- NOIP2007 提高组 复赛 game 矩阵取数游戏
- js选中值