python学习笔记_集合的定义和常用方法
1、认识集合
定义:
s={1,2,3,4,5}
s=set("hello")
s=set(["steven","job","dave"])

用set和可迭代对象进行定义时,相当于执行了for循环
补充:集合属于可变类型,如果定义不可变类型的集合用frozenset(),
如s=frozenset("hello")
print(s) #结果为:frozenset({'h', 'o', 'l', 'e'})
特点:
不同元素组成
无序
集合中的元素必须是不可变 (字符串,数字,元祖)
2、常用方法
.add("a") #增加一个元素
.clear() #清空
.copy() #浅拷贝
.pop() #随机删
.remove("job") #指定删,删除元素不存在时会报错
.discard("job") #指定删,删除元素不存在时不会报错
3、基本运算
交集,并集,差集
a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
print(a_s,b_s)
#求交集
print(a_s.intersection(b_s)) #写法1
print(a_s&b_s) #写法2
#求并集
print(a_s.union(b_s)) #写法1
print(a_s|b_s) #写法2
#求差集,相当于存在于左边,但不存在于右边的集合
print(a_s.difference(b_s)) #写法1
print(a_s-b_s) #写法2
print(b_s-a_s)
结果:
{'dave', 'steve', 'max', 'job'} {'dave', 'mark', 'job'}
{'dave', 'job'}
{'dave', 'job'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'steve', 'max'}
{'steve', 'max'}
{'mark'}
4、其他方法
a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
c_s={"job","dave"}
#交叉补集 ,交集的补集,相当于并集减去交集的部分的集合
print(a_s.symmetric_difference(b_s)) #写法1
print(a_s^b_s) #写法2
#结果为:{'mark', 'max', 'steve'}
#.difference_update()
a_s.difference_update(b_s) # 相当于 a_s=a_s-b_s
print(a_s)
#结果为:{'max', 'steve'}
#.isdisjoint() 判断有没有交集,没有交集返回true
print(a_s.isdisjoint(b_s))
#结果为:false
#.issubset() 是否是某个集合的子集。相当于是s1<=s2
print(c_s.issubset(a_s)) #写法1
print(c_s<=a_s) #写法2
#.issuperset() 是否是某个集合的父集。相当于是s1>=s2
print(a_s.issuperset(c_s))
#结果为 true
#.update() 可以增加多个值。可以传元祖,列表等可迭代对象
a_s.update(b_s)
print(a_s)
b_s.update(("bob","zhou"))
print(b_s)
#结果为:
#{'steve', 'job', 'max', 'dave', 'mark'}
#{'bob', 'mark', 'job', 'dave', 'zhou'}
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
"""
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set. This has no effect if the element is already present.
"""
pass def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass def difference(self, *args, **kwargs): # real signature unknown
"""
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.)
"""
pass def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member. If the element is not a member, do nothing.
"""
pass def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
"""
pass def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
pass def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
pass def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
"""
pass def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.)
"""
pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. """
pass def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
passseset
set
python学习笔记_集合的定义和常用方法的更多相关文章
- Python学习笔记_我的参考网址
Python学习笔记, 下面记录网上搜到的可参考的网址: 一.关于Tkinter 1.Python3中tkinter模块使用方法详解 https://blog.csdn.net/Fighting_Bo ...
- python学习笔记整理——集合 set
python学习整理笔记--集合 set 集合的用途:成员测试和消除重复的条目,进行集合运算 注意:花括号或set()函数可以用于创建集合. 注意:若要创建一个空的集合你必须使用set(),不能用{} ...
- Python学习笔记-Day3-set集合操作
set集合,是一个无序且不重复的元素集合.定义方式类似字典使用{}创建 目前我们学过的数据类型: 1.字符串(str),2.整型(int),3.浮点型(float),4,列表(list) 5.元组(t ...
- python学习笔记(集合的使用)
集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...
- python学习笔记(7)——集合(set)
关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...
- 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素
import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...
- Python学习笔记7—集合
set 拥有类似 dict 的特点:可以用{}花括号来定义:其中的元素没有序列,也就是是非序列类型的数据;而且,set 中的元素不可重复,这就类似 dict 的键. >>> s1 = ...
- 【Python学习笔记】集合
概述 集合的一般操作 内建函数进行标准操作集合 数学运算符进行标准操作集合 集合的应用 概述 python的集合(set)是无序不重复元素集,是一种容器.集合(set)中的元素必须是不可变对象,即可用 ...
- Python学习笔记 - function调用和定义
调用函数: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>> abs(100) 100 >>> a ...
随机推荐
- 五十一、SAP中使程序结构化,模块化
一.我们创建3个引用单元,这3个单元里面分别存放定义相关,执行相关和子程序相关的内容 二.将程序中的各部分都拆分到INCLUDE文件中,DEF文件内容如下 三.INC文件内容 四.MAIN文件中写程序 ...
- 【转】美团 MySQL 数据实时同步到 Hive 的架构与实践
文章转载自公众号 美团技术团队 , 作者 萌萌 背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的 ...
- Python MongoDB 创建集合
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Linux基础命令-02
Linux基础命令-02:
- 重新修改AD中PCB的形状快捷键
Altium Designer 快速修改板子形状为Keep-out layer大小 1,切换到 Keep-out layer层, 2,选择层,快捷键为S+Y: 3,设计-板子形状-按照选择 ...
- LeetCode简单题汇总
1.两个数之和 给出一个整数数组,请在数组中找出两个加起来等于目标值的数, 你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index ...
- Eclipse 快速打开文件所在的本地目录
目前收集到两种方法: 1.快捷键:Ctrl+Shift+W 2.利用Eclipse的External Tools设置快捷方式 第二种方法步骤: a.Run->External Tools-> ...
- Docker部署NETCORE应用程序
Docker部署NETCORE应用程序 创建.netcore项目,本文以ASP.NET Core2.2 版本为例 设置Dockerfile属性始终复制 编辑Dockerfile内容 FROM micr ...
- 学术Essay写作中Introduction的正确打开方式
其实在学术essay写作过程中,很多留学生经常不知道如何写introduction,所以有些开头的模板句就出现了,比如,With the development of society/With the ...
- tortoiseGit 的简单使用说明
拉取仓库到本地 参考 下面几张图片,把仓库拉取到本地. 本地修改并推送 进入文件夹后,按照 下面几张图片切换到本地的开发分支 当修改完成之后,按照 下面几张图片 的方法把修改推送到远程仓库的开发分支. ...