Python 集合

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']
set 函数方法
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)         #要添加的集合
posted @ 2023-06-12 11:33  evescn  阅读(9)  评论(0编辑  收藏  举报