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 '单引 ...
随机推荐
- SpringBoot Mybatis-Plus 整合 dynamic-datasource-spring-boot-starter 对数据库进行读写分离
准备工作 对 MySql 进行主从搭建 引入 dynamic-datasource-spring-boot-starter 坐标 引入 druid-spring-boot-starter 坐标 对应框 ...
- java 之 abstract、interface
abstract (抽象) 用abstract关键字来修饰一个类时,这个类叫做抽象类: 用abstract来修饰一个方法时,该方法叫做抽象方法. 抽象方法:只有方法的声明,没有方法的实现.以分号结束: ...
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
- P1457 城堡 The Castle 位运算+BFS+思维(难题,好题)
题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张"幸运爱尔兰"(一种彩票).结果这张彩票让他获得了这次 ...
- 大batch任务对structured streaming任务影响
信念,你拿它没办法,但是没有它你什么也做不成.—— 撒姆尔巴特勒 前言 对于spark streaming而言,大的batch任务会导致后续batch任务积压,对于structured streami ...
- 前端【JS】,深入理解原型和原型链
对于原型和原型链,相信有很多伙伴都说的上来一些,但有具体讲不清楚.但面试的时候又经常会碰到面试官的死亡的追问,我们慢慢来梳理这方面的知识! 要理解原型和原型链的关系,我们首先需要了解几个概念:1.什么 ...
- Cordova 浅析架构的原理
因为项目使用了Cordova,也使用了很长时间.至于有很多hybride框架,为什么我们使用Cordova,这里不做过多的叙述,我们也是根据项目需求来选定的,需要及时更新.还要输出别人SDK等.没有最 ...
- 201771010113 李婷华 《面向对象程序设计(Java)》第六周总结
一.理论知识部分 第四章 类与对象 1.方法的定义:方法声明和方法体. 2.重载:一个类中可以有多个方法具有相同的名字,不同的类型,不同的参数. 3.构造器:也叫构造方法,是类中的一种特殊的方法,其作 ...
- 美团分布式ID生成框架Leaf源码分析及优化改进
本文主要是对美团的分布式ID框架Leaf的原理进行介绍,针对Leaf原项目中的一些issue,对Leaf项目进行功能增强,问题修复及优化改进,改进后的项目地址在这里: Leaf项目改进计划 https ...
- ubuntu安装java方法
详情请点链接:https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-1 ...