[转]python集合set
2015-10-17 15:34
302 查看
Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种。创建集合set、集合set添加、集合删除、交集、并集、差集的操作都是非常实用的方法。
>>>set('boy')
set(['y', 'b', 'o'])
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])
简单的演示下差集、交集和合集的概念:
来源网址:http://www.iplaypython.com/jichu/set.html
创建集合
set类是在python的sets模块中,大家现在使用的python2.3中,不需要导入sets模块可以直接创建集合。>>>set('boy')
set(['y', 'b', 'o'])
集合添加、删除
集合的添加有两种常用方法,分别是add和update。集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])
python集合操作符号、数学符号
集合的交集、合集(并集)、差集,了解集合set的这些非常好用的功能前,要先了解一些集合操作符号简单的演示下差集、交集和合集的概念:
相关文章推荐
- Python多进程
- PYTHON-进阶-装饰器小结
- pip安装whl格式的python库
- 笨办法学python的笔记之二
- Python 字符串
- python list(列表)操作用法总结
- python 自己写的计算工资的小程序
- <LeetCode><Easy> 28 Remove Element
- Python内置的字符串处理函数
- Python 调试工具 PDB
- python List 介绍
- python 一个小的进程监控程序
- <LeetCode><Easy> 26 Remove Duplitcates from Sorted Array
- python strip()函数
- Python 内建函数--map()
- Learn Python The Hard Way 习题41详解
- <LeetCode><Easy> 21 MergeTwoSortedLists
- 笨办法学python的笔记之一
- Python集合(set)类型的操作
- Python: 去掉字符串中的非数字(或非字母)字符