Python 集合set()添加删除、交集、并集、集合操作详解
在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种。创建集合set、集合set添加、集合删除、交集、并集、差集的操作都是非常实用的方法。
玩蛇网python之家提示: 推荐在Linux系统下编写python程序,会养成一个非常好的编码习惯。
创建集合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的这些非常好用的功能前,要先了解一些集合操作符号
简单的演示下差集、交集和合集的概念:
可变集合python set是www.iplaypy.com玩蛇网python学习交流平台,初期python学习中比较能接触到的。像列表、字典、字符串这类可迭代的对像都可以做为集合的参数。set集合是无序的,不能通过索引和切片来做一些操作。
浏览这篇文章的网友,正在看:
Python 100例 练习题
树莓派python编程
正则表达式
JSON教程
Apache配置
MySQL数据库
Python标签页
更多推荐页面:
python招聘2页,3页,4页,5页,6页,7页,8页,9页,10页,11页,12页,13页
seo与python_2页,3页,4页
python资讯2页,3页
python源码2页,3页,4页,5页,6页
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/jichu/set.html
相关文章 Recommend
- • 使用django在做添加superuser操作时报错\xBA\xA3像是乱
- • 想把print输出后的空格删除怎么做
- • 不小心删除了pythonwin,重装之后提示不是有效的
- • python rss框架内部做添加数据操作遇到的问题
- • Python字典操作的问题,要删除字典内部重复值ite
- • 这个python项目中向原数据中追加添加新数据用什
- • python2.7函数删除一个全局变量为什么不报SyntaxE
- • Python删除目标词所在行的数据怎么操作
- • python setup.py方式安装包正确删除方法
- • python删除dict空值元素报错RuntimeError原因
- • 求python删除相连空格并保留唯一的方法
- • Python urllib2.Request.add_header() 添加header不成功解决
PYTHON基础入门必备教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
PYTHON进阶提高必备教程 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Pyhton 单行、多行注释符号使用方法及规范
- • Python List 列表list()方法
- • Python QQ群 基础编程开发qq群 python编程交
- • Python type()函数用途及使用方法
- • Python idle安装与使用教程 调试、下载
- • Python 集合set()添加删除、交集、并集、集
- • Python 函数是什么?如何定义、调用函数
- • Python 变量的定义命名规范和赋值
- • Python int 数字整型类型 定义int()范围大小
- • Python 元组遍历排序操作方法
图文精华 RECOMMEND
-
Pyhton 单行、多行注释符号使用方
-
Python List 列表list()方法
-
Python QQ群 基础编程开发qq群 pyt
-
Python type()函数用途及使用方法
-
Python idle安装与使用教程 调试、下
-
Python 集合set()添加删除、交集、并
热点文章 HOT
- Python List 列表list()方法
- Python 类class定义 方法与属性教程
- Python 元组遍历排序操作方法
- Python 映射类型字典dict操作定义
- Python 函数是什么?如何定义、调用函数
- Python 标准操作符与逻辑运算符
- Python 集合set()添加删除、交集、并集、集
- Python idle安装与使用教程 调试、下载
- Python基本内置数据类型有哪些?
- Python 字符串是什么及简单操作方法