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

最新文章

  1. 深入学习jQuery描述文本内容的3个方法
  2. Mysql查询——学习阶段
  3. 读书笔记之深入理解Nginx:模块开发与结构解析
  4. poj2676 Sudoku
  5. ping过程
  6. jquery选择器之属性选择器
  7. 【最后一篇API译文】Android开发-API指南- Contacts Provider
  8. Zencart 国家排序及中文名称的扩展
  9. uboot的mtd功能支持
  10. 用VBA读取Excel表格输出到格式化的xml文件中
  11. Qt窗口的标题栏自绘
  12. 自己写CPU第四阶段(2)——验证该第一指令ori实现效果
  13. oracle_安装_win7+64位+Oracle+11g+64位下使用PLSQL+Developer+的解决办法
  14. Unity NGUI UIPanel下对粒子的剪裁
  15. [转载] Netty教程
  16. cadence电源和地平面的处理
  17. MySQL--Insert Buffer
  18. 通信——基于Xmpp协议实现的聊天室
  19. IC卡冷复位时序
  20. LINUX内核分析第七周学习总结

热门文章

  1. CSS样式----CSS的继承性和层叠性(图文详解)
  2. Android学习笔记-EditText(输入框)(二)
  3. Java 字符串截取问题
  4. iOS多款源码分享
  5. 10. leetcode 226 Invert Binary Tree
  6. 剑指offer--二叉树的后序遍历
  7. 【某集训题解】【DAY 2 T3】与非
  8. Typescript 解构 、展开
  9. 只需要一点点C++基础,新手也可以制作单机游戏内存修改器
  10. recycleView 使用指南1