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. UWP:使用Behavior实现Button点击动态效果

    废话不多说,先上效果 没有做成安卓那种圆形的原因是...人家真的不会嘛... 好了下面是正文: 首先在工程中引入Behavior的库,我们使用Nuget. 在项目->引用上点击右键,点击管理Nu ...

  2. ABP从入门到精通(1):aspnet-zero-core项目启动及各项目源码说明

    一.ABP的简单介绍 ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践 ...

  3. (转)systemctl 命令完全指南

    场景:在使用chkconfig查看vsftpd是否看机启动时候看不到启动项,用systemctl 才看到自己想要的结果 1 总结 from:https://linux.cn/article-5926- ...

  4. EF 数据库迁移(Migration)

    Update-Database -ConnectionStringName "MyConnectionString"

  5. pwnable.kr col之write up

    Daddy told me about cool MD5 hash collision today. I wanna do something like that too! ssh col@pwnab ...

  6. Python 获取当前路径的方法

    Python2.7 中获取路径的各种方法 sys.path 模块搜索路径的字符串列表.由环境变量PYTHONPATH初始化得到. sys.path[0]是调用Python解释器的当前脚本所在的目录. ...

  7. C#设计模式(1)-单例模式

    单例(Singleton)模式介绍 单例模式:也可以叫单件模式,官方定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式的特点: 单例类只能有一个实例. 单例类必须自己创建自己的唯一 ...

  8. S2-032代码执行

    Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结构差别 ...

  9. RabbitMQ入门-消息订阅模式

    消息派发 上篇<RabbitMQ入门-消息派发那些事儿>发布之后,收了不少反馈,其中问的最多的还是有关消息确认以及超时等场景的处理. 楼主,有遇到消费者后台进程不在,但consumer连接 ...

  10. python--DenyHttp项目(2)--ACM监考服务器端

    服务器端: #coding:utf-8 ''' ServerGui.py 设置比赛开始时间 设置比赛结束时间 若时间无误启动监听服务 ''' import time import re import ...