set

set集合,是一个无序且不重复的元素集合

  • set的优势
set 的访问数度快
set 原生解决数据重复问题
# 数据库中原有
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 }
} s1=set()
for i in old_dict.keys():
s1.add(i)
print(s1) s2=set()
for j in new_dict.keys():
s2.add(j)
print(s2) s3=s2.difference(s1)
print(s3) for n in s3:
s=new_dict.get(n) old_dict.update({n:s}) print(old_dict)
Help on set object:
class set(object)
| set() -> new empty set object
| set(iterable) -> new set object
|
| Build an unordered collection of unique elements.
|
| Methods defined here:
|
| __and__(self, value, /)
| Return self&value.
|
| __contains__(...)
| x.__contains__(y) <==> y in x.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __gt__(self, value, /)
| Return self>value.
|
| __iand__(self, value, /)
| Return self&=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __ior__(self, value, /)
| Return self|=value.
|
| __isub__(self, value, /)
| Return self-=value.
|
| __iter__(self, /)
| Implement iter(self).
|
| __ixor__(self, value, /)
| Return self^=value.
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __or__(self, value, /)
| Return self|value.
|
| __rand__(self, value, /)
| Return value&self.
|
| __reduce__(...)
| Return state information for pickling.
|
| __repr__(self, /)
| Return repr(self).
|
| __ror__(self, value, /)
| Return value|self.
|
| __rsub__(self, value, /)
| Return value-self.
|
| __rxor__(self, value, /)
| Return value^self.
|
| __sizeof__(...)
| S.__sizeof__() -> size of S in memory, in bytes
|
| __sub__(self, value, /)
| Return self-value.
|
| __xor__(self, value, /)
| Return self^value.
|
| add(...)
| Add an element to a set.
|
| This has no effect if the element is already present. '''向set里面添加元素,若为重复元素则不会添加''' |
| clear(...)
| Remove all elements from this set. '''清空set里面的所有元素''' |
| copy(...)
| Return a shallow copy of a set.
'''浅拷贝''' |
| difference(...)
| 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.) '''对比两个或多个set,将不相同的元素放到新的set当中, 例:A、B两个集合对比,将A中存在B中不存在的元素返回到一个新的集合中
>>> s1={1,2,3,4}
>>> s2={1,2}
>>> s3=s1.difference(s2)
>>> print(s3)
{3, 4}
'''
|
| difference_update(...)
| Remove all elements of another set from this set. '''从当前集合中删除和B中相同的元素. 注:直接修改当前集合
>>> s1={1,2,3,4}
>>> s2={1,2,}
>>> s1.difference_update(s2)
>>> print(s1)
{3, 4} ''' |
| discard(...)
| Remove an element from a set if it is a member.
|
| If the element is not a member, do nothing. '''如果元素属于集合,删除当前元素,如果不属于,不变
>>> s1={1,2,3,4}
>>> s1.discard(9)
>>> print(s1)
{1, 2, 3, 4}
>>> s1.discard(1)
>>> print(s1)
{2, 3, 4}
''' |
| intersection(...)
| Return the intersection of two sets as a new set.
|
| (i.e. all elements that are in both sets.) '''
取两个集合的交集,返回给一个新的集合 >>> s1={1,2,3,4}
>>> s2={1,2,5,6}
>>> s3=s1.intersection(s2)
>>> print(s3)
{1, 2} ''' |
| intersection_update(...)
| Update a set with the intersection of itself and another.
|
'''
取两个集合的交集,赋值给当前集合
>>> s1={1,2,3,4}
>>> s2={1,2,5,6}
>>> s1.intersection_update(s2)
>>> print(s1)
{1, 2}
''' | isdisjoint(...)
| Return True if two sets have a null intersection.
'''
判断两个集合是否有交集,如果有返回False,没有则返回Ture
>>> s1={1,2,3,4}
>>> s2={1,2,5,6}
>>> s3={9,10}
>>> s1.isdisjoint(s2)
False
>>> s1.isdisjoint(s3)
True
'''
|
| issubset(...)
| Report whether another set contains this set.
'''
判断前者是不是后面集合的子集,是:Tute,否:False
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s3={1,2}
>>> s1.issubset(s2)
True
>>> s1.issubset(s3)
False
'''
|
| issuperset(...)
| Report whether this set contains another set.
'''
判断前者是否是后者的父集,是:Tute,否:False
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s3={1,2}
>>> s1.issuperset(s2)
False
>>> s1.issuperset(s3)
True
'''
|
| pop(...)
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty.
'''
删除集合中的元素,并返回被删除的元素(随机删除?)
'''
|
| remove(...)
| Remove an element from a set; it must be a member.
|
| If the element is not a member, raise a KeyError. '''
删除指定的一个元素,如果不存在,报KeyError错误
'''
|
| symmetric_difference(...)
| Return the symmetric difference of two sets as a new set.
|
| (i.e. all elements that are in exactly one of the sets.)
'''
差集:将两个集合中不相同的元素返回到一个新的集合中
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s3=s1.symmetric_difference(s2)
>>> print(s3)
{5, 6, 7}
'''
|
| symmetric_difference_update(...)
| Update a set with the symmetric difference of itself and another.
'''
差集:将两个集合中不同的元素赋值给当前集合
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s1.symmetric_difference_update(s2)
>>> print(s1)
{5, 6, 7}
'''
|
| union(...)
| Return the union of sets as a new set.
|
| (i.e. all elements that are in either set.)
'''
并集:取两个集合的并集复制给新的集合
>>> s1={1,2,3,4}
>>> s2={4,5,6,7}
>>> s3=s1.union(s2)
>>> print(s3)
{1, 2, 3, 4, 5, 6, 7}
'''
|
| update(...)
| Update a set with the union of itself and others.
'''
更新:将后面的集合的元素添加到当前集合
>>> s1={1,2,3,4,"yangge"}
>>> s2={1,2,3,4,5,6,7}
>>> s1.update(s2)
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7, 'yangge'}
''' |
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
None

Python之set的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  4. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  5. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  6. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  7. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  8. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  9. python开发编译器

    引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...

  10. 关于解决python线上问题的几种有效技术

    工作后好久没上博客园了,虽然不是很忙,但也没学生时代闲了.今天上博客园,发现好多的文章都是年终总结,想想是不是自己也应该总结下,不过现在还没想好,等想好了再写吧.今天写写自己在工作后用到的技术干货,争 ...

随机推荐

  1. javascript实现朴素贝叶斯分类与决策树ID3分类

    今年毕业时的毕设是有关大数据及机器学习的题目.因为那个时间已经步入前端的行业自然选择使用JavaScript来实现其中具体的算法.虽然JavaScript不是做大数据处理的最佳语言,相比还没有优势,但 ...

  2. java调用wkhtmltopdf生成pdf文件,美观,省事

    最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...

  3. [js] post 方式打开新窗口

    一.前因 一般我们是用 window.open(url,name,params); 打开新窗口, url 会携带一些参数, 但存在参数过多,引发url 过长截断,无法打开正确窗口, 所以我们需要使用 ...

  4. Java实现简单文件过滤器

    输入路径查找该路径下的指定文件类型的文件 代码思路: 想要循环遍历文件夹下所有子文件夹,就要用到递归. 首先判断路径是否存在: 是:获取文件 判断是否文件夹: 是:调用自身,继续获取子文件夹下内容 否 ...

  5. 《深入理解Java虚拟机》虚拟机类加载机制

    上节学习回顾 上一节,我们深入到类文件去了解其结构细节,也大概对类文件的编写规则略知一二了,解析来我们就得学习这个类文件是如何被加载到Java虚拟机的,看看有什么引人入胜的奥秘. 本节学习重点 大部分 ...

  6. STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'

    一种是在系统开始--运行里输入cmd,查看armcc状态.详情见推文: http://blog.csdn.net/hicui/article/details/7350805(笔记记录,请勿见怪) 都没 ...

  7. 再起航,我的学习笔记之JavaScript设计模式04

    我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 上回 ...

  8. gitlab 实现自动部署(简单Python实现)

    功能说明: 当本地master分支执行push动作的时候,服务器端会自动执行master分支的pull操作(还可以执行一些自动化脚本) 原理: git hooks就是那些在git执行特定事件(如com ...

  9. Eclipse创建Maven项目报错的解决

    报错1:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart 起因:删除一个用quick ...

  10. openstack使用openvswitch实现vxlan组网

     openstack使用openvswitch实现vxlan openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点 ...