python基本數據類型之集合set


一、集合的定義

set集合,是一個無序且不重復的元素集合。

集合對象是一組無序排列的可哈希的值,集合成員可以做字典中的鍵。集合支持用in和not in操作符檢查成員,由len()內建函數得到集合的基數(大小), 用 for 循環迭代集合的成員。但是因為集合本身是無序的,不可以為集合創建索引或執行切片(slice)操作,也沒有鍵(keys)可用來獲取集合中元素的值。

二、集合的創建

s = set()
s = {11,22,33,44}

*注:創建空集合時,只能用set(),如果用第二種方法s={},創建的實際上是一個空字典。
s = {}
print(type(s))
<class 'dict'>
a=set('boy')
b=set(['y', 'b', 'o','o'])
c=set({"k1":'v1','k2':'v2'})
d={'k1','k2','k2'}
e={('k1', 'k2','k2')}
print(a,type(a))
print(b,type(b))
print(c,type(c))
print(d,type(d))
print(e,type(e))
執行結果如下:
{'o', 'b', 'y'} <class 'set'>
{'o', 'b', 'y'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{('k1', 'k2', 'k2')} <class '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. A中存在,B中不存在
         
        (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.  從當前集合中刪除和B中相同的元素"""
        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.  取交集並更更新到A中 """
        pass
 
    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection.  如果沒有交集,返回True,否則返回False"""
        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. 對稱交集,並更新到a中 """
        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
源碼

基本功能:

  • 增加
a=set('python')
a.add('tina')
print(a)
b=set('python')
b.update('tina')
print(b)
執行結果如下:
{'tina', 'o', 'p', 'n', 't', 'y', 'h'}
{'o', 'i', 'p', 'a', 'n', 't', 'y', 'h'}
##################
由以上代碼可以看出,add是單個元素的添加,而update是批量的添加。輸出結果是無序的,並非添加到尾部。
  • 刪除(remove,discard,pop)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.remove('p')
print(c)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.discard('p')
print(c)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.pop()
print(c)
執行結果如下: {'i', 'h', 't', 'o', 'y', 'n'} 
#####
當執行c.remove('p','i')和c.discard('p','i')時,報錯:TypeError: remove() takes exactly one argument (2 given),說明remove和discard刪除元素時都只能一個一個的刪,同add對應。
#################################################################################
remove,pop和discard的區別:
discard刪除指定元素,當指定元素不存在時,不報錯;
remove
刪除指定元素,但當指定元素不存在時,報錯:KeyError。
pop刪除任意元素,並可將移除的元素賦值給一個變量,不能指定元素移除。
  • 清空
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.clear()
print(c)
執行結果如下:
set()

set的特有功能:

s1 = {0}
s2 = {i % 2 for i in range(10)}
s = set('hi')
t = set(['h', 'e', 'l', 'l', 'o'])
print(s.intersection(t), s & t)  # 交集
print(s.union(t), s | t)   # 並集
print(s.difference(t), s - t)  # 差集
print(s.symmetric_difference(t), s ^ t) # 對稱差集
print(s1.issubset(s2), s1 <= s2) # 子集(被包含)
print(s1.issuperset(s2), s1 >= s2)   # 父集(包含)

執行結果如下:
{'h'} {'h'}
{'i', 'e', 'h', 'l', 'o'} {'i', 'e', 'h', 'l', 'o'}
{'i'} {'i'}
{'e', 'l', 'o', 'i'} {'e', 'l', 'o', 'i'}
True True
False False
s = {11,22,33}
t = {22,44}
print(s.isdisjoint(t))#(disjoint脫節的,)即如果沒有交集,返回True,否則返回False
s.difference_update(t)#將差集覆蓋到源集合,即從當前集合中刪除和B中相同的元素
print(s)
執行結果如下:
False
{33, 11}

s = {11,22,33}
t = {22,44}
s.intersection_update(t)#將交集覆蓋到源集合
print(s)
執行結果如下:
{22}

s = {11,22,33}
t = {22,44}
s.symmetric_difference_update(t)#將對稱差集覆蓋到源集合
print(s)
執行結果如下:
{33, 11, 44}

四、集合的轉換

se = set(range(4))
li = list(se)
tu = tuple(se)
st = str(se)
print(li,type(li))
print(tu,type(tu))
print(st,type(st))
執行結果如下:
[0, 1, 2, 3] <class 'list'>
(0, 1, 2, 3) <class 'tuple'>
{0, 1, 2, 3} <class 'str'>

 五、練習題

 尋找差異:哪些需要刪除?哪些需要新建?哪些需要更新?

# 數據庫中原有
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 }
} 

注意:無需考慮內部元素是否改變,只要原來存在,新匯報也存在,就是需要更新

del_dict = set(old_dict).difference(set(new_dict))
add_dict = set(new_dict).difference(set(old_dict))
update_dict = set(new_dict).intersection(set(old_dict))
print(del_dict)
print(add_dict)
print(update_dict)
執行結果如下:
{'#2'}
{'#4'}
{'#3', '#1'}
#!/usr/bin/python
# -*- coding:utf-8 -*-
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},
}


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},
}
new_set = set()
old_set = set()
for i in new_dict:
    new_set.add(i)
for j in old_dict:
    old_set.add(j)
new_add = new_set.difference(old_set)    #new_dict中有,old_dict中沒有
old_del = old_set.difference(new_set)    #old_dict中有,new_dict中沒有
update = new_set.intersection(old_set)    #old_dict和new_dict共同有的,需要把new_dict更新到old_dict中

for k in new_add:
    old_dict[k] = new_dict[k]    #將new_dict中新增的內容添加到old_dict中
for v in old_del:
    del old_dict[v]    #將old_dict中失效的內容刪除
for m in update:
    old_dict[m] = new_dict[m]    #把new_dict更新到old_dict中

print(old_dict)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM