set使用方法
set 添加一个无序的,用set方法,访问速度快,天生解决了重复问题
1、difference 指定某个元素从原来set取出,并生成新的set
#difference
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.difference(["aaa"])
print(b) {'ccc', 'bbb', 'aaa'}
{'ccc', 'bbb'}
2、difference_update 删除原来set里某个元素
1 #difference_update
2 a = set(["aaa","bbb","ccc","aaa"])
3 print(a)
4 b = a.difference_update(["aaa"])
5 print(a)
6 print(b)
7
8
9 {'bbb', 'ccc', 'aaa'}
10 {'bbb', 'ccc'}
11 None
3、pop 删除某个元素
#pop
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.pop()
print(a)
print(b) {'aaa', 'bbb', 'ccc'}
{'bbb', 'ccc'}
aaa
4、remove 删除指定元素
#remove
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.remove("ccc")
print(a)
print(b) {'bbb', 'ccc', 'aaa'}
{'bbb', 'aaa'}
None
这里举一个例子,更新、删除、添加原来的表
old_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#2": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80}
}
# cmdb 新汇报的数据
new_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 800},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#4": {'hostname': 'c2', 'cpu_count': 2, 'mem_capicity': 80}
}
a = set(old_dict.keys())
b = set(new_dict.keys())
#要更新的数据
update_set = a.intersection(b)
print(update_set)
#要删除的数据
delete_set = a.symmetric_difference(update_set)
print(delete_set)
#要添加的数据
add_set = b.symmetric_difference(update_set)
print(add_set)
{'#3', '#1'}
{'#2'}
{'#4'}
set简单说明:
1 class set(object):
2 """
3 set() -> new empty set object
4 set(iterable) -> new set object
5
6 Build an unordered collection of unique elements.
7 """
8 def add(self, *args, **kwargs): # real signature unknown
9 """
10 Add an element to a set,添加元素
11
12 This has no effect if the element is already present.
13 """
14 pass
15
16 def clear(self, *args, **kwargs): # real signature unknown
17 """ Remove all elements from this set. 清除内容"""
18 pass
19
20 def copy(self, *args, **kwargs): # real signature unknown
21 """ Return a shallow copy of a set. 浅拷贝 """
22 pass
23
24 def difference(self, *args, **kwargs): # real signature unknown
25 """
26 Return the difference of two or more sets as a new set. A中存在,B中不存在
27
28 (i.e. all elements that are in this set but not the others.)
29 """
30 pass
31
32 def difference_update(self, *args, **kwargs): # real signature unknown
33 """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
34 pass
35
36 def discard(self, *args, **kwargs): # real signature unknown
37 """
38 Remove an element from a set if it is a member.
39
40 If the element is not a member, do nothing. 移除指定元素,不存在不保错
41 """
42 pass
43
44 def intersection(self, *args, **kwargs): # real signature unknown
45 """
46 Return the intersection of two sets as a new set. 交集
47
48 (i.e. all elements that are in both sets.)
49 """
50 pass
51
52 def intersection_update(self, *args, **kwargs): # real signature unknown
53 """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """
54 pass
55
56 def isdisjoint(self, *args, **kwargs): # real signature unknown
57 """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
58 pass
59
60 def issubset(self, *args, **kwargs): # real signature unknown
61 """ Report whether another set contains this set. 是否是子序列"""
62 pass
63
64 def issuperset(self, *args, **kwargs): # real signature unknown
65 """ Report whether this set contains another set. 是否是父序列"""
66 pass
67
68 def pop(self, *args, **kwargs): # real signature unknown
69 """
70 Remove and return an arbitrary set element.
71 Raises KeyError if the set is empty. 移除元素
72 """
73 pass
74
75 def remove(self, *args, **kwargs): # real signature unknown
76 """
77 Remove an element from a set; it must be a member.
78
79 If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
80 """
81 pass
82
83 def symmetric_difference(self, *args, **kwargs): # real signature unknown
84 """
85 Return the symmetric difference of two sets as a new set. 对称差集
86
87 (i.e. all elements that are in exactly one of the sets.)
88 """
89 pass
90
91 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
92 """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
93 pass
94
95 def union(self, *args, **kwargs): # real signature unknown
96 """
97 Return the union of sets as a new set. 并集
98
99 (i.e. all elements that are in either set.)
100 """
101 pass
102
103 def update(self, *args, **kwargs): # real signature unknown
104 """ Update a set with the union of itself and others. 更新 """
105 pass
set使用方法的更多相关文章
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 【.net 深呼吸】细说CodeDom(6):方法参数
本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...
- IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法
直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
- ArcGIS 10.0紧凑型切片读写方法
首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...
- [BOT] 一种android中实现“圆角矩形”的方法
内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...
- JS 判断数据类型的三种方法
说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
随机推荐
- WPF实现三星手机充电界面
GitHub地址:https://github.com/ptddqr/wpf-samsung-phone-s5-charging-ui/tree/master 先上效果图 这个效果来自于三星S5的充电 ...
- REDIS持久化报错失败
redis log报错: [7666] 15 Jan 00:22:36.028 # Error moving temp DB file on the final destination: Invali ...
- AngularJS指令
1. AngularJS指令的特点: AngularJS通过被称为指令的新属性来扩展HTML,指令的前缀为ng-. AngularJS通过内置的指令来为应用添加功能. AngularJS允许你自定义指 ...
- python venv下安装mysql出错 解决方法
1.首先使用exe文件安装python-mysql.链接: http://pan.baidu.com/s/1kVqILTX 密码: manj. 2.虚拟环境创建后,我们把已经在公共环境使用exe安装好 ...
- win7下安装mysql后修改密码
mysql的安装教程网上很多,此处不过多介绍,个人觉得下面这篇教程是比较好的,一步到位.MySQL 5.7.9 ZIP 免安装版本配置过程_百度经验 http://jingyan.baidu.com ...
- 82.Android之MVP+Retrofit+RxJava实践小结
转载:http://wuxiaolong.me/2016/06/12/mvpRetrofitRxjava/ 关于MVP.Retrofit.RxJava,之前已经分别做了分享,如果您还没有阅读过,可以猛 ...
- Jquery获取offsetHeight
纯javascript: window.document.getElementById('MainColumn').offsetHeight jquery: $('#id').get(0).offse ...
- Linux问题处理: stdio.h: No such file or directory linux
本来打算编译一下<自制编程语言>一书的代码,结果提示错误: 其实说的还是很清楚的,一般出现这种情况都是没有安装相应的库,所以: 再次编译,成功: 测试代码: # comment print ...
- Beta阶段项目终审报告
先上图 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要是用来解决玩狼人杀这款桌游时无牌.无法官.游戏流程不熟悉等情况的.我觉得我们对典型 ...
- Xamarin.ios 调用接口
NSString urlstring = new NSString("http://211.155.229.230:8585/api/users/postregister"); ...