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使用方法的更多相关文章

  1. javaSE27天复习总结

    JAVA学习总结    2 第一天    2 1:计算机概述(了解)    2 (1)计算机    2 (2)计算机硬件    2 (3)计算机软件    2 (4)软件开发(理解)    2 (5) ...

  2. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  3. 【.net 深呼吸】细说CodeDom(6):方法参数

    本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...

  4. IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法

    直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...

  5. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  6. [C#] C# 基础回顾 - 匿名方法

    C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...

  7. ArcGIS 10.0紧凑型切片读写方法

    首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...

  8. [BOT] 一种android中实现“圆角矩形”的方法

    内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...

  9. JS 判断数据类型的三种方法

    说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...

  10. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

随机推荐

  1. 基于modelsim-SE的简单仿真流程—上

    基于modelsim-SE的简单仿真流程 编写RTL功能代码 要进行功能仿真,首先得用需要仿真的模块,也就是RTL功能代码,简称待测试的模块,该模块也就是在设计下载到FPGA的电路.一个电路模块想要有 ...

  2. kubernetes 文档

    kubernetes 官方文档:http://kubernetes.io/docs/ null

  3. 文件上传之——用SWF插件实现文件异步上传和头像截取

    之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...

  4. 使用css3做钟表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. [转]ExtJS Grid 分页时保持选中的简单实现方法

    原文地址 :http://www.qeefee.com/article/ext-grid-keep-paging-selection ExtJS中经常要用到分页和选择,但是当选择遇到分页的时候,杯具就 ...

  6. 如何使用PullToRefresh

    这里有详解 PullToRefresh使用详解(一)--构建下拉刷新的listView PullToRefresh使用详解(二)---重写BaseAdapter实现复杂XML下拉刷新 PullToRe ...

  7. Http client 请求

    public String sendPost(String url, String param) { System.out.println("------------------ 1&quo ...

  8. Java中判断字符串是否为数字的五种方法

    //方法一:用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ ...

  9. js中this关键字测试集锦

    参考:阮一峰<javascript的this用法>及<JS中this关键字详解> this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象,只能在 ...

  10. 架构师养成记--9.future模式讲解

    什么是future模式呢?解释这个概念之前我们先来了解一个场景吧,财务系统的结账功能,这个功能可能是每个月用一次,在这一个月中相关的数据量已经积累得非常大,这一个功能需要调用好几个存储过程来完成.假如 ...