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中ALV事件之六,复制一个标准工具栏到自己的程序
一.我们来到SE41,点击复制状态按钮 二.点击复制状态后,弹出一个框框,上面是模板内容,下面是我们自己的程序 三.我们根据上一篇的标准模板内容,填好相应的模板和我们的程序的内容 三.点击复制按钮 五 ...
- Meeloun教你如何正式切入Essay写作话题
很多同学在Essay写作过程中会发现:如果题目问到解决办法,写来写去,都是政府要颁布政策,人们要提高意识,感觉一点新意也没有.怎么样更好地切合不同的话题,想到最合适的解决办法呢?今天小编为你奉上更多处 ...
- java方法中使用js的alert。
out.println("<script type=\"text/javascript\">"); out.println("alert( ...
- Java高级特性——注解,这也许是最简单易懂的文章了
最近,浪尖在做flink的项目时source和sink的绑定那块用到了注解,当然新版本1.6以后就变为server load的方式加载. 但是浪尖也是觉得很有毕业讲一下注解,毕竟高级免试也会问答的吧. ...
- VUE swiper.js引用使用轮播图
<template> <div class="home"> <div class="swiper-container"> & ...
- GS 原理及破解 《0day安全》
1.原理: 在main函数之前,会调用__security_init_cookie函数(win10,vs2017,release,x86): 进入__security_init_cookie函数内部: ...
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- PHP ~ 通过程序删除图片,同时删除数据库中的图片数据 和 图片文件
删除单张图片 <?php require_once '../../conn.php'; //连接数据库 $ID = $_GET['ID' ...
- html使用aes进行加密
1.导入 aes.js 文件 !function(t,n){*t.length},toString:function(t){);o<r;o++){]>>>-o%*&;n ...
- 使用navicat连接mysql8.0.12版本 出现client does not support。。。解决办法
navicat版本的问题 出现连接失败的原因:mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password ...