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 '单引 ...
随机推荐
- python学习11函数
'''''''''函数:1.定义:指通过专门的代码组织,用来实现特定的功能的代码段,具有相对独立性,可供其他代码重复调用2.语法:def 函数名([参数]): 函数体[return 返回值]3.函数名 ...
- Display a QMessageBox from a QThread
Emit a signal. Since you cannot do UI stuff in a Qthread, instead send your message as an argument o ...
- Linux磁盘修复命令----fsck
使用fsck命令修复磁盘时 一定要进入单用户模式去修复 语 法fsck.ext4[必要参数][选择参数][设备代号] 功 能fsck.ext4 命令: 针对ext4型文件系统进行检测 参数 -a 非 ...
- 听说你在从事前端开发?那这10个JavaScript的优化问题你不得不知道!
JavaScript的高效优化一直都是我们前端开发中非常重要的工作,也是很多开发人员无法做好的一部分内容,今天我总结了10个优化问题,大家可以参考来做优化,这其中很多问题都是大家经常遇到的哦. ==和 ...
- Font-Awesome使用教程
何为Font-Awesome Font Awesome gives you scalable vector icons that can instantly be customized — size, ...
- element UI排坑记(一):判断tabs组件是否切换
之所以将这个问题列在排坑记之中,是因为官方组件的一个属性颇有些隐蔽,这个问题曾经折腾了本人较多时间,始终思维固着,且使用搜索引擎也不容易搜索到答案,故记之.然而实际解决却是相当简单的. 一.问题描述 ...
- 用两张图告诉你,为什么你的App会卡顿?
有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? 知道Android究竟是如何在屏幕上显示我们期望的画面的? 对Android的视图架构有整体把握. 学会 ...
- CentOS 7 编译错误解决方法集合
解决 error: the HTTP XSLT module requires the libxml2/libxslt 错误 yum -y install libxml2 libxml2-dev yu ...
- python selenium(定位方法)
一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...
- 题目分享R
题意:有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最短时间和最长时间. 分析:(其实 ...