Python之set

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
时间: 2024-08-25 01:31:27

Python之set的相关文章

Python学习1-Python和Pycharm的下载与安装

本文主要介绍Python的下载安装和Python编辑器Pycharm的下载与安装. 一.Python的下载与安装 1.下载 到Python官网上下载Python的安装文件,进入网站后显示如下图: 网速访问慢的话可直接在这里下载:python-2.7.11.amd64 在Downloads中有对应的支持的平台,这里我们是在Windows平台下运行,所以点击Windows,出现如下: 在这里显示了Python更新的所有版本,其中最上面两行分别是Python2.X和Python3.X对应的最后更新版本

Python——深入理解urllib、urllib2及requests(requests不建议使用?)

深入理解urllib.urllib2及requests            python Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年,Python 源代码同样遵循 GPL(GNU General Public License)协议[1] .Python语法简洁而清晰,具有丰富和强大的类库. urllib and urllib2 区别 urllib和urllib2模块都做与请求URL相关的操作,但

python学习_day26_面向对象之封装

1.私有属性 (1)动态属性 在python中用双下划线开头的方式将属性隐藏起来.类中所有双下划线开头的名称,如__x都会自动变形成:_类名__x的形式.这种自动变形的特点是: a.类中定义的__x只能在内部使用,如self.__x,引用的就是变形的结果.b.这种变形其实正是针对外部的变形,在外部是无法通过__x这个名字访问到的.c.在子类定义的__x不会覆盖在父类定义的__x,因为子类中变形成了:_子类名__x,而父类中变形成了:_父类名__x,即双下滑线开头的属性在继承给子类时,子类是无法覆

python面向对象知识点疏理

面向对象技术简介 类: 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例.class 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且在函数体之外.类变量通常不作为实例变量使用. 数据成员:类变量或者实例变量用于处理类及其实例对象的相关的数据. 方法重写:如果从父类继承的方法不能满足子类的需求,可以对其进行改写,这个过程叫方法的覆盖,也称为方法的重写. 实例变量:定义在方法中的变量,只作用于当前实例的类. 继承:即一个派生类(de

python实现网页登录时的rsa加密流程

对某些网站的登录包进行抓包时发现,客户端对用户名进行了加密,然后传给服务器进行校验. 使用chrome调试功能断点调试,发现网站用javascript对用户名做了rsa加密. 为了实现网站的自动登录,需要模拟这个加密过程. 网上搜了下关于rsa加密的最简明的解释: rsa加密是非对称加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥.公钥是可发布的供任何人使用,私钥则为自己

Python中编码的详细讲解

看这篇文章前,你应该已经知道了为什么有编码,以及编码的种类情况 ASCII 占1个字节,只支持英文 GB2312 占2个字节,支持6700+汉字 GBK GB2312的升级版,支持21000+汉字 Shift-JIS 日本字符 ks_c_5601-1987 韩国编码 TIS-620 泰国编码 由于每个国家都有自己的字符,所以其对应关系也涵盖了自己国家的字符,但是以上编码都存在局限性,即:仅涵盖本国字符,无其他国家字符的对应关系.应运而生出现了万国码,他涵盖了全球所有的文字和二进制的对应关系, U

Python练习(一)

Python练习(一): 给一个不超过5位的正整数,判断其有几位,依次打印出个位.十位.百位.千位.万位的数字: num = int(input('please enter a number: '))   lst = [] for i in str(num):      lst.append(i) lenlst = len(lst) if num >= 1000:      if num >= 10000:          print('too big')     else:        

菜鸟学python之对象类型及运算

Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型. 等号(=)用来给变量赋值. 1 变量赋值 1.1 单个变量赋值 >>> name="python" >>> print(name) python 1.2 多个变量赋值 >>> name=names="python&

开始我的Python爬虫学习之路

因为工作需要经常收集一些数据,我就想通过学爬虫来实现自动化完成比较重复的任务. 目前我Python的状况,跟着敲了几个教程,也算是懂点基础,具体比较深入的知识,是打算从做项目中慢慢去了解学习. 我是觉得如果一开始就钻细节的话,是很容易受到打击而放弃的,做点小项目让自己获得点成就感路才更容易更有信心走下去. 反正遇到不懂的就多查多问就对了. 知乎上看了很多关于入门Python爬虫的问答,给自己总结出了大概的学习方向. 基础: HTML&CSS,JOSN,HTTP协议(这些要了解,不太需要精通) R

解决:Elipse配置Jython Interpreters时报错Error: Python stdlib source files not found

今天学习lynnLi的博客monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)时,遇到了一个问题,即: lynnLi给出的解决办法是:将Python下的Lib拷贝到sdk中jython-standalone-2.5.3.jar所在目录tools\lib,再次New一个Jython,添加jython-standalone-2.5.3.jar路径即可 但是这样尝试,又报错了: 后来借鉴了这篇文章的做法,成功配置Jython Interpreters: 将\sdk