python3.x 基础三:set集合
集合,set(),记住:
1个特点:去重,把列表变成集合,达到自动去重操作,无序
5个关系:测试两个列表的交差并子反向差集
方法:
- | add(...) 常用,已存在元素去重不生效
- | Add an element to a set.
- | This has no effect if the element is already present.
>>> list1=[3,2,1,1,2,3,4,5]
>>> set(list1)
{1, 2, 3, 4, 5}
>>> list2=[3,4,5,6,7,8]
>>> set(list1).add(2)
>>> set(list1).add(6)
>>> print(set(list1).add(2))
None
>>> print(set(list1).add(6))
None
>>> set1=set(list1)
>>> set2=set(list2)
>>> set1,set2
({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
>>> set1.add(3)
>>> print(set1.add(3)
... )
None
>>> print(set1.add(7))
None
>>> set1.add('aaa')
>>> set1
{1, 2, 3, 4, 5, 7, 'aaa'}
>>> id(set1)
140138768484616
>>> set1.add('aaaa')
>>> id(set1)
140138768484616
>>> set1
{1, 2, 3, 4, 5, 7, 'aaa', 'aaaa'}
>>> set1.add('')
>>> set1
{1, 2, 3, 4, 5, 7, 'aaa', 'aaaa', ''}
>>> set1.add(7)
>>> set1
{1, 2, 3, 4, 5, 7, 'aaa', 'aaaa', ''} - 如果是字符串,则拆分成单个字符集合
>>> set('abc')
{'a', 'c', 'b'}
- | clear(...) 清空一个集合
- | Remove all elements from this set.
>>> set1.clear()
>>> set1
set() - | copy(...) 影子复制,指向同一个内存地址
- | Return a shallow copy of a set. |
>>> list1
[3, 2, 1, 1, 2, 3, 4, 5]
>>> list2
[3, 4, 5, 6, 7, 8]
>>> set1=set(list1)
>>> id(set1)
140138768485512>>> set3=set1.copy()
>>> id(set3)
140138695576712 - | difference(...) 差集,格式set1.difference(set2),求in list1 not in list2的集合
- | Return the difference of two or more sets as a new set. |
- | (i.e. all elements that are in this set but not the others.)
>>> set1
{1, 2, 3, 4, 5}
>>> set2
{3, 4, 5, 6, 7, 8}
>>> set1.difference(set2)
{1, 2} - | difference_update(...) 删除在本集合同时也在其他集合的元素,差集
- | Remove all elements of another set from this set. |
>>> set1=set(list1)
>>> set2=set(list2)
>>> set1,set2
({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
>>> set1=set(list1)
>>> set2=set(list2)
>>> set2.difference_update(set1)
>>> set2
{6, 7, 8} - | discard(...) 删除一个在本集合中的元素
- | Remove an element from a set if it is a member.
- |
- | If the element is not a member, do nothing. |
>>> set3=set([1,2,3,'a','b','c'])
>>> set3.discard(1)
>>> set3
{2, 3, 'c', 'b', 'a'}
>>> set3.discard('a')
>>> set3
{2, 3, 'c', 'b'}
>>> set3.discard('dd')
>>> set3
{2, 3, 'c', 'b'} - | intersection(...)并集,同时在两个集合中的元素
- | Return the intersection of two sets as a new set.
- |
- | (i.e. all elements that are in both sets.) |
>>> set1
{1, 2, 3, 4, 5}
>>> set2
{3, 4, 5, 6, 7, 8}
>>> set1.intersection(set2)
{3, 4, 5}
>>> set2.intersection(set1)
{3, 4, 5} - | intersection_update(...) 交集
- | Update a set with the intersection of itself and another. |
>>> set1,set2
({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
>>> set1.intersection_update(set2)
>>> set1
{3, 4, 5} - | isdisjoint(...) 返回布尔值,判断两个集合是否没有并集
- | Return True if two sets have a null intersection.
- |
>>> set1,set2,set3,set4
({3, 4, 5}, {3, 4, 5, 6, 7, 8}, {2, 3, 'c', 'b'}, {'y', 'x', 'z'})
>>> set1.isdisjoint(set2)
False
>>> set1.isdisjoint(set4)
True - | issubset(...) 返回布尔值,判断前一个集合是否是后一个集合的子集
- | Report whether another set contains this set. |
>>> set1
{3, 4, 5}
>>> set5
{3, 4}
>>> set5.issubset(set1)
True - | issuperset(...) 返回布尔值,判断前一个集合是否是后一个集合的父集
- | Report whether this set contains another set. |
>>> set1
{3, 4, 5}
>>> set5
{3, 4}
>>> set1.issuperset(set5)
True - | pop(...) 随机删除一个集合元素,返回被删除元素,空集合删除则报错
- | Remove and return an arbitrary set element.
- | Raises KeyError if the set is empty. |
>>> set1
{3, 4, 5}
>>> set1.pop()
3
>>> set1
{4, 5}
>>> set1.pop()
4
>>> set1.pop()
5
>>> set1.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set' - | remove(...) 删除指定在集合中的元素
- | Remove an element from a set; it must be a member. |
>>> set1=set(list1)
>>> set1
{1, 2, 3, 4, 5}
>>> set1.remove(1)
>>> set1
{2, 3, 4, 5}
>>> set1.remove('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a' - | symmetric_difference(...) 对称差集,集合A与集合B不相交的部分,交集的反集
- | Return the symmetric difference of two sets as a new set.
- | (i.e. all elements that are in exactly one of the sets.)
>>> set1
{1, 2, 3, 4, 5}
>>> set6={4,5,6,7,8}
>>> set1.symmetric_difference(set6)
{1, 2, 3, 6, 7, 8}
>>> set6.symmetric_difference(set1)
{1, 2, 3, 6, 7, 8}
>>> set1,set7
({1, 2, 3, 4, 5}, {'a', 'c', 'b'})
>>> set1.symmetric_difference(set7)
{'c', 2, 3, 1, 4, 5, 'b', 'a'}
- | symmetric_difference_update(...)
- | Update a set with the symmetric difference of itself and another.
- |
- | union(...) 并集
- | Return the union of sets as a new set.
- | (i.e. all elements that are in either set.)
>>> set1
{1, 2, 3, 4, 5}
>>> set2
{3, 4, 5, 6, 7, 8}
>>> set1.union(set2)
{1, 2, 3, 4, 5, 6, 7, 8}
- | update(...) 用交集更新到set1的集合
- | Update a set with the union of itself and others. |
>>> set1
{1, 2, 3, 4, 5}
>>> set2
{3, 4, 5, 6, 7, 8}
>>> set1.update(set2)
>>> set1
{1, 2, 3, 4, 5, 6, 7, 8}
>>>
python3.x 基础三:set集合的更多相关文章
- python3.x 基础三:装饰器
装饰器:本质是函数,用于装饰其他函数,在不改变其他函数的调用和代码的前提下,增加新功能 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 3.装饰函数对于被装饰函数透明 参考如 ...
- python3.x 基础三:函数
1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程变成,万事皆过程,def定义过程 3.函数式编程,将某种功能封装起来,用的时候直接调用函数名,def定义函数,也叫f ...
- python3.x 基础三:字符集问题
总结了一张表,更详细信息百度百科: 序号 年份 编码 标准协会 特点 二进制长度 字符长度 表现 1 1967 ASCII 美国国家标准学会(American National Standard In ...
- python基础三(集合、文件)
1.集合定义 集合天生能去重,且与字典一样,无序.集合用大括号括起来,里面的元素之间用逗号分隔,要跟字典区分开. 集合定义方法:s=set() #定义一个空集合 s={'1','a','b','c', ...
- python3.x 基础三:文件IO
打开文件的两种方式 1.直接打开文件并赋值给变量,打开后得到操作句柄,但不会自动关闭 file = open('文件名‘,'打开模式',’编码‘) fd = open('../config/file1 ...
- Python全栈开发【基础三】
Python全栈开发[基础三] 本节内容: 函数(全局与局部变量) 递归 内置函数 函数 一.定义和使用 函数最重要的是减少代码的重用性和增强代码可读性 def 函数名(参数): ... 函数体 . ...
- Bootstrap <基础三十一>插件概览
在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...
- C#基础课程之五集合(HashTable,Dictionary)
HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...
- Ruby语法基础(三)
Ruby语法基础(三) 在前面快速入之后,这次加深对基本概念的理解. 字符串 Ruby字符串可以分为单引号字符串和双引号字符串,单引号字符串效率更高,但双引号的支持转义和运行 puts '单引 ...
随机推荐
- Inno Setup打包之先卸载再安装
使用Inno Setup打包程序之后,如果想要在安装前先卸载,那么需要加下面代码,需要注意的是红色标注的改为你们自己的.网上看到有些说_is1前面用AppName,但是我这边验证不行. [Setup] ...
- js 运动函数篇 (一) (匀速运动、缓冲运动、多物体运动、多物体不同值运动、多物体多值运动)层层深入
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写 匀速运动.缓冲运动.多物体运 ...
- java 之 jsp详解
jsp所需环境 eclipse JSP/Servlet 环境 jsp处理 以下步骤表明了 Web 服务器是如何使用JSP来创建网页的: 就像其他普通的网页一样,您的浏览器发送一个 HTTP 请求给服务 ...
- python连接mysql数据表查询表获取数据导入到txt中
import pymysql'''连接mysql数据表查询表获取数据导入到txt中'''#查询结果写入数据到txtdef get_loan_number(file_txt): connect = py ...
- CTO为何要微服务评估
为什么定义参考模型 之前我的工作,大部分时间都是聚焦在某个产品/团队,为他们提供微服务/DevOps的实施及指导.进入公司后,同时参与了多个产品团队的改造研讨.其中最大的不同在于: 在面对一个团队的时 ...
- PPT模板素材
http://588ku.com/sucai/0-dnum-0-54-0-1/
- bfs—迷宫问题—poj3984
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20591 Accepted: 12050 http://poj ...
- CentOS7.x上轻量级TCP转发工具rinetd的安装配置
一.实验背景 Linux下端口转发一般都使用iptables来实现,使用iptables可以很容易将TCP和UDP端口从防火墙转发到内部主机上. 如果需要将流量从专用地址转发到不在您当前网络上的机器上 ...
- JWT的浅谈
在实际工作过程中,运行jmeter脚本的时候,开发给了一个jwt的授权信息,到底是做什么用的呢,翻阅了一些资料,整理如下: 一.JWT(Json Web Token)是什么 JWT是一串格式为xxxx ...
- LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...