Python之set

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于Python之set的主要内容,如果未能解决你的问题,请参考以下文章

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

30 段 Python 实用代码

Python之如何优雅的重试

即学即用的 30 段 Python 实用代码

python之集合set

python之模块和包