Python学习【第八篇】Set集合
Set集合
set集合是无序,不能重复,可嵌套的序列
如何创建
li = []
dic = {"k1":123}
se = {"123","456"} # 查看它的类型
print (type(se)) # 输出
<class 'set'> --------------------------------------------------------------
# 创建: s1 = {11,22} s2 = set() # 创建一个空集合 s3 = set([11,22,33,44]) ---------------------------------------------------------------
# set集合无序,且不重复的序列 li = [11,22,11,33]
s4 = set(li)
print(s4) # 输出
{33, 11, 22}
功能
·添加元素
s = set()
s.add(123)
print(s) # 输出
{123} # PS:添加相同元素,不会生效,因为之前说过set是不重复的序列
s.add(123)
s.add(123)
print(s) # 输出
{123}
·清楚所有内容
s.clear()
print(s) # 输出
set()
·浅拷贝
s.add(456)
s1 = s.copy()
print(s1) # 输出
{456}
·取不同
s1 = {11,22,33}
s2 = {22,33,44}
----------------------------------------------------------
# s1中存在,s2中不存在
s3 = s1.difference(s2)
print(s3)
# 输出
{11}
----------------------------------------------------------
# s1中存在,s2中不存在,并更新到s1
s1.difference_update(s2)
print(s1)
# 输出
{11}
-----------------------------------------------------------
# 取s1,s2中不同的元素
s4 = s1.symmetric_difference(s2)
print(s4)
# 输出
{11,44}
------------------------------------------------------------
# 取s1,s2中不同的元素,并更新到s1
s1.symmetric_difference_update(s2)
print(s1)
# 输出
{11,44}
·移除
#移除指定元素(不存在不报错)
s1 = {11,22,33}
s1.discard(11)
print(s1) # 输出
{33, 22} --------------------------------------------------- # 移除指定元素(但报错)
s1.remove(1111)
print(s1) --------------------------------------------------- # 随机移除
s2 = {11,22,33}
ret = s2.pop()
print(ret)
print(s2) # 输出
33
{11,22} ·交集 s1 = {11,22,33}
s2 = {22,33,44}
s3 = s1.intersetion(s2)
print(s3) # 输出
{33, 22} ------------------------------------------- # 计算出交集直接更新到s1
s1.intersection_update(s2)
print(s1) # 输出
{33,22}
·并集
s1 = {11,22,33}
s2 = {22,33,44}
s3 = s1.union(s2)
print(s3)
# 输出
{33, 22, 11, 44}
·更新
# 批量更新可以迭代的对象
s1 = {11,22,33}
li = [44,22,33,11,55] # 更新列表
s1.update(li)
print(s1) # 输出
{33, 11, 44, 22, 55} ------------------------------------------------- # 更新字符串
s1 = {11,22,33}
a = "boubon"
s1.update(a)
print(s1) # 输出
{33, 'n', 11, 'o', 'u', 22, 'b'}
class set(object):
"""
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. A中存在,B中不存在 (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. 从当前集合中删除和B中相同的元素"""
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. 取交集并更更新到A中 """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
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. 对称差集,并更新到a中 """
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. 更新 """
pass
set(object)
类CMDB的小实验
old_dic = {
"#1": 8,
"#2": 4,
"#4": 2,
}
new_dic = {
"#1": 4,
"#2": 4,
"#3": 2,
}
'''
应该删除哪几个槽位
需求分析:
1.old_dic存在,new_dic不存在的key
old_keys = old_dic.keys()
old_set = set(old_keys)
new_keys = new_dic.keys()
new_set = set(new_keys)
old_set.differents(new_set)
应该更新哪几个槽位
应该增加哪几个槽位
'''
old_keys = set(old_dic.keys())
new_keys = set(new_dic.keys())
remove_set = old_keys.difference(new_keys)
add_set = new_keys.difference(old_keys)
update_set = old_keys.intersection(new_keys)
Python学习【第八篇】Set集合的更多相关文章
- Python 学习 第八篇:函数2(参数、lamdba和函数属性)
函数的参数是参数暴露给外部的接口,向函数传递参数,可以控制函数的流程,函数可以0个.1个或多个参数:在Python中向函数传参,使用的是赋值方式. 一,传递参数 参数是通过赋值来传递的,传递参数的特点 ...
- Python学习笔记进阶篇——总览
Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...
- Python学习笔记基础篇——总览
Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...
- python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍
目录 python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍.md 一丶字典 1.字典的定义 2.字典的使用. 3.字典的常用方法. python学习第八讲,python ...
- Python 学习 第十篇 CMDB用户权限管理
Python 学习 第十篇 CMDB用户权限管理 2016-10-10 16:29:17 标签: python 版权声明:原创作品,谢绝转载!否则将追究法律责任. 不管是什么系统,用户权限都是至关重要 ...
- Python学习笔记——基础篇【第七周】———类的静态方法 类方法及属性
新式类和经典类的区别 python2.7 新式类——广度优先 经典类——深度优先 python3.0 新式类——广度优先 经典类——广度优先 广度优先才是正常的思维,所以python 3.0中已经修复 ...
- Python 学习笔记---基础篇
1. 简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200 import subprocess cmd="cmd.exe" b ...
- Python学习系列提升篇------字符串
字符串是python学习中重要的内容,在以后的工作中,对字符串的处理也必少不了.下面总结一下关于字符串学习的经验. 1.1 字符串的连接和合并 用‘ + ’连接,将两个字符串相加. 合并, ...
- python学习第十五天集合的创建和基本操作方法
集合是python独有的数据列表,集合可以做数据分析,集合是一个无序的,唯一的的数据类型,可以确定列表的唯一性,说一下集合的创建和基本常见操作方法 1,集合的创建 s={1,2,4} 也可以用set( ...
- Python 学习 第14篇:数据类型(元组和集合)
元组和集合是Python中的基本类型 一,元组 元组(tuple)由小括号.逗号和数据对象构成的集合,各个项通过逗号隔开,元组的特点是: 元组项可以是任何数据类型,也可以嵌套 元组是一个位置有序的对象 ...
随机推荐
- mybatis 书写
查询语句是使用 MyBatis 时最常用的元素之一 select元素配置细节如下 属性 描述 取值 默认 id 在这个模式下唯一的标识符,可被其它语句引用 parameterType 传给此语 ...
- 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】
一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...
- PIL中分离通道发生“AttributeError: 'NoneType' object has no attribute 'bands'”
解决方法: 这个貌似是属于一个bug 把Image.py中的1500行左右的split函数改成如下即可: def split(self): "Split image into bands&q ...
- Marshal的简单使用
终于从北京回上海了,第一次听unity开发者大会,感觉讲的都是一些Unity 5新功能的介绍,其实主要还是要靠自己去摸索那些新的功能,主要就是添加了新的GUI系统,貌似集成了NGUI到Unity中,取 ...
- EpochConverter
地址:http://www.epochconverter.com/ How to get the current epoch time in ... PHP time() more ... Pytho ...
- 桶装水 送水 消费充值PDA会员管理系统 介绍
桶装水 送水 消费充值PDA会员管理系统 介绍 主要功能:会员管理临时开卡.新增会员.修改会员.删除会员场馆管理仓管信息管理.租凭信息管理会员卡管理会员卡类型设置.会员发卡.会员信息管理.体验用户发卡 ...
- [BZOJ4027][HEOI2015] 兔子与樱花
Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接 ...
- uva10986 堆优化单源最短路径(pas)
var n,m,s,t,v,i,a,b,c:longint;//这道题的代码不是这个,在下面 first,tr,p,q:..]of longint; next,eb,ew:..]of longint; ...
- 使用WWW获取本地文件夹的XML配置文件
Unity3D读取本地文件可以使用Resources.Load来读取放在Resources文件夹下的文件,如果不是放在该文件夹下,则可以通过WWW类来读取. 譬如读取xml的配置文件. /// < ...
- iOS PCH文件
在Xcode6之前,创建一个新的工程,Xcode会再Support Files文件夹下自动创建一个"工程名 - prefix.pch"文件,也是一个头文件,pch文件的内容能被项目 ...