首页 > 编程语言 >Python 集合

Python 集合

时间:2023-06-12 11:47:38浏览次数:34  
标签:__ set Python self pass 集合 s1 def

Set 数据类型

一、创建一个 set

# set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
# 访问速度快,天生解决了重复问题

>>> s1 = set(['root', 'gm', 'evescn', 'gm'])

二、查看 set

>>> s1
{'gm', 'evescn', 'root'}

三、查看 set 可进行的操作

>>> dir(s1)
['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
list 函数方法
class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
    
    Build an unordered collection of unique elements.
    """
    def add(self, *args, **kwargs): # real signature unknown
        """
        Add an element to a set.
        
        This has no effect if the element is already present.
        """
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. """
        pass

    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a set. """
        pass

    def difference(self, *args, **kwargs): # real signature unknown
        """
        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.)
        """
        pass

    def difference_update(self, *args, **kwargs): # real signature unknown
        """ Remove all elements of another set from this set. """
        pass

    def discard(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set if it is a member.
        
        If the element is not a member, do nothing.
        """
        pass

    def intersection(self, *args, **kwargs): # real signature unknown
        """
        Return the intersection of two sets as a new set.
        
        (i.e. all elements that are in both sets.)
        """
        pass

    def intersection_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the intersection of itself and another. """
        pass

    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection. """
        pass

    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set. """
        pass

    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty.
        """
        pass

    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set; it must be a member.
        
        If the element is not a member, raise a KeyError.
        """
        pass

    def symmetric_difference(self, *args, **kwargs): # real signature unknown
        """
        Return the symmetric difference of two sets as a new set.
        
        (i.e. all elements that are in exactly one of the sets.)
        """
        pass

    def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the symmetric difference of itself and another. """
        pass

    def union(self, *args, **kwargs): # real signature unknown
        """
        Return the union of sets as a new set.
        
        (i.e. all elements that are in either set.)
        """
        pass

    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. """
        pass

    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass

    def __class_getitem__(self, *args, **kwargs): # real signature unknown
        """ See PEP 585 """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iand__(self, *args, **kwargs): # real signature unknown
        """ Return self&=value. """
        pass

    def __init__(self, seq=()): # known special case of set.__init__
        """
        set() -> new empty set object
        set(iterable) -> new set object
        
        Build an unordered collection of unique elements.
        # (copied from class doc)
        """
        pass

    def __ior__(self, *args, **kwargs): # real signature unknown
        """ Return self|=value. """
        pass

    def __isub__(self, *args, **kwargs): # real signature unknown
        """ Return self-=value. """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __ixor__(self, *args, **kwargs): # real signature unknown
        """ Return self^=value. """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass

    def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass

    def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass

    def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass

    def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass

    __hash__ = None

四、常用的 set 操作

  • add
# 添加一个新的key

>>> s1
{'gm', 'evescn', 'root'}
>>> s1.add('gm')
>>> s1
{'gm', 'evescn', 'root'}
  • 集合比较操作
# s1和s2对比
>>> s1
{'gm', 'evescn', 'root'}
>>> s2
{'gm', 'root', 'hlr'}

>>> s1.difference(s2)     # s1差s2
{'evescn'}
>>> s2.difference(s1)     # s2差s1
{'hlr'}
>>> s1.intersection(s2)   # s1交s2
{'gm', 'root'}
>>> s1.union(s2)          # s1并s2    
{'gm', 'root', 'hlr', 'evescn'}
  • difference和difference_update的区别
# difference把差异结果返回,但不修改原集合
# difference_update修改原集合# 其他操作,例如:intersection和intersection_update也是类似的
>>> s1
{'gm', 'evescn', 'root'}
>>> s2
{'gm', 'root', 'hlr'}

>>> s2.difference(s1)
{'hlr'}
>>> s3 = s2.difference(s1)
>>> s3        # s3获取到了difference返回的结果
{'hlr'}
>>> s2        # s2未被修改
{'gm', 'root', 'hlr'}

>>> s4 = s2.difference_update(s1)
>>> s4        # difference_updat没有返回结果
>>> s2        # s2已经被修改
{'hlr'}
  • pop和remove的区别

# pop:删除一个key值,并且删除的key可以使用变量来获取,不支持删除指定的key值
# remove:删除指定的key值,不能获取到删除的key值,不指定key值会报错

>>> s1
{'gm', 'evescn', 'root', 'abc'}

>>> set1 = s1.pop('gm')        # 指定key值报错
Traceback (most recent call last):
  File "<pyshell#158>", line 1, in <module>
    set1 = s1.pop('gm')
TypeError: pop() takes no arguments (1 given)

>>> set1 = s1.pop()
>>> set1        # 获取到删除的key
'gm'

>>> s1
{'evescn', 'root', 'abc'}
>>> set2 = s1.pop()
>>> set2
'evescn'

>>> s1
{'root', 'abc'}
>>> set3 = s1.remove()        # 删除时,不指定key值报错
Traceback (most recent call last):
  File "<pyshell#152>", line 1, in <module>
    set3 = s1.remove()
TypeError: remove() takes exactly one argument (0 given)

>>> set3 = s1.remove('abc')
>>> set3        # 不能获取到删除的key
>>> s1
{'root'}
# 差集和对称差的区别
>>> s1
{'gm', 'evescn', 'root'}
>>> s2
{'gm', 'hlr', 'root'}

# 差集
>>> ret1 = s1.difference(s2)    
>>> ret2 = s2.difference(s1) 

>>> print(ret1)
{'evescn'}
>>> print(ret2)
{'hlr'}

# 简单的理解如何计算出差集的
for item in s1:
    if item in s2:
        del "s1.item"

# 对称差
ret1 = s1.symmetric_difference(s2)    
ret2 = s2.symmetric_difference(s1) 

>>> print(ret1)
{'evescn', 'hlr'}
>>> print(ret2)
{'evescn', 'hlr'}

# 如何计算出对称差的
for item in s1:
    if item not in s2:
        add "ret1.item"

for item in s2:
    if item not in s1:
        add "ret1.item"

五、练习:寻找差异

# 数据库中原有
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 }
}

需要删除:?
需要新建:?
需要更新:? 注意:无需考虑内部元素是否改变,只要原来存在,新汇报也存在,就是需要更新
  • 结果
交集、差集即为需要的结果

交集:
    需要更新或不变的数据

差集:
    old差new  -->  删除的数据
    new差old  -->  新增的数据

# 获取字典的key值
old = set(old_dict.keys())
new = set(new_dict.keys())

update_set = old.intersection(new)           #要更新的集合
delete_set = old.difference(update_set) )    #要删除的集合
add_set = new.difference(update_set)         #要添加的集合

标签:__,set,Python,self,pass,集合,s1,def
From: https://www.cnblogs.com/evescn/p/17474601.html

相关文章

  • python学习笔记35-脚本有多个文件时如何指定import_path
    如果脚本涉及多个文件,且分布在多个目录中,则会涉及到import其它目录的文件.如果脚本不在脚本所在的目录run,则会涉及到run目录与脚本目录不相关.如何解决这个问题呢?假设目录结构如下:/a/b/c/bin/dfx.py/a/b/c/atpg/atpg.py/a/b/c/atpg/atpg_sim.py/a/b/c/common/my_......
  • Python工具箱系列(三十五)
    前文使用了SQLAlchemy的Core层来实现数据库、表与数据的CRUD。初步体现出了SQLAlchemy的优势。但ORM的特点没有充分地表现出来。下面的代码则从Python的类出现,生成表结构,并且进行数据的CRUD操作。fromsqlalchemyimport(Column,DateTime,Float,ForeignKey,Integer,MetaDa......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1-11fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()6defdraw(n,size=100):7foriinrange(n):8fd(size)9left(360/n)10defmain():11pensize(2)12pencolor(......
  • python学习笔记33-将标准输出打印到文件中
    importsys#保存原始的stdout,方便后续恢复out_tmp=sys.stdout#将stdout重新定向到文件,这样输出的标准输出的内容就会输出到文件中sys.stdout=open('help.QWidget.rpt','w')#执行help命令,标准输出打印内容,此时会打印到文件中fromPyQt5.QtWidgetsimport......
  • python学习笔记34-获取函数的help信息
    list_for_help=list()list_for_help.append("PyQt5.QtCore")list_for_help.append("PyQt5.QtCore.QTime")list_for_help.append("PyQt5.QtGui")list_for_help.append("PyQt5.QtGui.QColor")list_for_help.append("PyQt5......
  • RC4加密算法及Python实现
    一、RC4加密算法原理RC4算法是一种流加密算法,由RonRivest在1987年设计。它的主要特点是简单快速,而且在加密解密过程中使用的密钥长度可变。因此,RC4算法被广泛应用于网络安全领域,如SSL、TLS、WEP、WPA等协议中。RC4算法的加密过程如下:初始化S盒和T数组。S盒是一个256字节的数组,用于......
  • Python工具箱系列(三十五)
    前文使用了SQLAlchemy的Core层来实现数据库、表与数据的CRUD。初步体现出了SQLAlchemy的优势。但ORM的特点没有充分地表现出来。下面的代码则从Python的类出现,生成表结构,并且进行数据的CRUD操作。fromsqlalchemyimport(Column,DateTime,Float,ForeignKey,Integer,MetaData,......
  • 掌握Python文件操作:从基础到高阶的全方位探索
    在本篇博客中,我们将全面、深入地探讨Python中的文件操作。文件操作在Python编程中是不可或缺的一部分,它包含了打开、读取、写入和关闭文件等各种操作。我们将从基础的文件操作讲解到高级的文件处理技巧,以及如何优雅地使用Python进行文件操作。每一部分我们都会分享一些独特的用法,并......
  • Python 推导式
    推导式简介Python推导式是一种别具一格的数据处理形式,可以由一个数据序列构建另一个数据序列,python中支持一下数据结构的推导式列表推导式元组推导式字典推导式集合推导式列表推导式列表推导式使用很频繁,也称为列表解释式,其作用使用一种优雅的形式来创建列表格式【表......
  • 【python基础】复杂数据类型-字典(增删改查)
    1.初识字典字典,是另外一种复杂的数据类型,相较于列表,字典可以将相关信息关联起来。比如说一个人的信息有名字、年龄、性别等,如果用列表存储的话,不能表示他们之间是相关联的,而字典可以,字典是一个或多个键值对组成列表的标志是[],字典的标志是{},其语法格式:{键1:值1,键2:值2,,,,,,......