Python set()函数详解
来源:互联网 发布:个人开淘宝食品店 编辑:程序博客网 时间:2024/06/06 20:38
在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种。创建集合set、集合set添加、集合删除、交集、并集、差集的操作都是非常实用的方法。
创建集合set
python set类是在python的sets模块中,大家现在使用的python2.7.x中,不需要导入sets模块可以直接创建集合。
>>>set('boy')
set(['y', 'b', 'o'])
集合添加、删除
python 集合的添加有两种常用方法,分别是add和update。
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
# 在学习python的朋友们,强烈推荐加入 Python QQ群。
集合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集合set与列表list的这些非常好用的功能前,要先了解一些集合操作符号
简单的演示下差集、交集和合集的概念:
阅读全文
0 0
- Python set()函数详解
- Python-set函数
- Python set()函数
- python: set函数
- Python-set函数
- python中set集合详解
- python dict 和set 详解
- Python的set集合详解
- python map set函数用法
- python property 与get/set方法详解
- python中set和frozenset详解
- Python中的list/tuple/dict/set详解
- Python中set函数的使用
- Python标准库:内置函数set([iterable])
- python基础III(dict、set、函数)
- python中set()函数的用法
- python--set集合下的函数
- 00106 python函数:set()集合
- gearman安装
- 贝叶斯线性回归小练习
- Spring bean三种创建方式
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- c++拷贝构造函数详解
- Python set()函数详解
- Spring bean注解配置(1)
- 剑指offer——二叉树的下一个节点___
- Spring 注解配置(2)——@Autowired
- Spring Boot 集成Swagger
- 遗传算法小结
- codevs 1725 探险(简单二分)
- 某标签在访问前后和鼠标悬浮时变色
- 【jzoj100006】【SDOI2017】【数字表格】【数论】