Python基礎(chǔ)(17)-set集合

set是一個無序且不重復(fù)的元素集合,set集合是不能被切片和被索引的。

17.1-創(chuàng)建set集合:

第一種方式:

>>> s1 = set([11,22,11,22])
>>> type(s1)
<type 'set'>

第二種方式:

>>> s1 = {11,22,11,22}
>>> type(s1)
<type 'set'>

注:如果里面沒有元素就是字典類型,若需要創(chuàng)建空的set集合,需使用第一種方式進行創(chuàng)建。

17.2-set集合內(nèi)部方法介紹:

add(self, *args, **kwargs):

說明:添加一個元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
print(s1)

# 添加一個元素
s1.add(44)
print(s1)

運行結(jié)果:

clear(self, *args, **kwargs):

說明:清空內(nèi)容。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
print(s1)

# 清空內(nèi)容
s1.clear()
print(s1)

運行結(jié)果:

copy(self, *args, **kwargs):

說明:淺拷貝。

difference(self, *args, **kwargs):

說明:返回一個新的 set 集合,包含原集合中有的,但傳入集合中沒有的元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
s2 = set([11,33,55])

# 原集合中有的,但傳入集合中沒有的元素
ret = s1.difference(s2)
print(ret)

運行結(jié)果:

difference_update(self, *args, **kwargs):

說明:刪除當(dāng)前 set 集合中所有包含在傳入集合中的元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
s2 = set([11,33,55])

# 刪除當(dāng)前集合中所有包含在傳入集合內(nèi)的元素
s1.difference_update(s2)
print(s1)

運行結(jié)果:

discard(self, *args, **kwargs):

說明:移除指定元素,所移除元素不存在將不會報錯。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
print(s1)

# 移除元素
s1.discard(22)
print(s1)

運行結(jié)果:

intersection(self, *args, **kwargs):

說明:取交集,創(chuàng)建一個新的 set 集合,即新建的 set 集合中包含兩集合的公共元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
s2 = set([11,33,44,55])

# 取交集,創(chuàng)建新的set集合
ret = s1.intersection(s2)
print(ret)

運行結(jié)果:

intersection_update(self, *args, **kwargs):

說明:取交集,更新原來的 set 集合。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
s2 = set([11,33,44,55])

# 取交集,更新原來的set集合
s1.intersection_update(s2)
print(s1)

運行結(jié)果:

isdisjoint(self, *args, **kwargs):

說明:判斷沒有交集,返回 True,否則,返回 False 。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
s2 = set([11,33,44,55])
s3 = set([66,77,88,99])

# 有交集,返回 False
ret1 = s1.isdisjoint(s2)
print(ret1)

# 沒有交集,返回 True
ret2 = s1.isdisjoint(s3)
print(ret2)

運行結(jié)果:

issubset(self, *args, **kwargs):

說明:判斷是否是子集。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,33])

# 判斷 s1 是否是 s2 的子集,即表示 s1 中所有元素是否都是 s2 的元素
ret1 = s1.issubset(s2)
print(ret1)

# 和 s1 <= s2 相同
ret2 = s1 <= s2
print(ret2)

print("--------" * 5)

# 判斷 s2 是否是 s1 的子集
ret3 = s2.issubset(s1)
print(ret3)

# 和 s2 <= s1 相同
ret4 = s2 <= s1
print(ret4)

運行結(jié)果:

issuperset(self, *args, **kwargs):

說明:判斷是否是父集。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,33])

# 判斷 s1 是否是 s2 的父集,即表示 s2 中的所有元素是否都包含在 s1 中
ret1 = s1.issuperset(s2)
print(ret1)

# 和 s1 >= s2 相同
ret2 = s1 >= s2
print(ret2)

print("--------" * 5)

# 判斷 s2 是否是 s1 的父集
ret3 = s2.issuperset(s1)
print(ret3)

# 和 s2 >= s1 相同
ret4 = s2 >= s1
print(ret4)

運行結(jié)果:

pop(self, *args, **kwargs):

說明:隨機移除集合內(nèi)的元素,并返回該元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])
print(s1)

# 隨機移除 s1 中的元素,并賦值給 ret
ret = s1.pop()
print(s1)
print(ret)

運行結(jié)果:

remove(self, *args, **kwargs):

說明:移除指定元素,所移除元素不存在將會報錯。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33,44])

# 所移除元素不存在將會報錯
s1.remove(55)
print(s1)

運行結(jié)果:

symmetric_difference(self, *args, **kwargs):

說明:取兩個集合的差集,創(chuàng)建一個新的 set 集合,即新建的 set 集合包含 s1 和 s2中不重復(fù)的元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,22,44,])

# 取差集,即s1 和 s2 中不相同的元素
ret1 = s1.symmetric_difference(s2)
print(ret1)

# 和 s1 ^ s2 相同
ret2 = s1 ^ s2
print(ret2)

運行結(jié)果:

symmetric_difference_update(self, *args, **kwargs):

說明:取兩個集合的差集,更新原來的 set 集合。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,22,44,])

# 取差集,即s1 和 s2 中不相同的元素,并更新 s1
s1.symmetric_difference_update(s2)
print(s1)

運行結(jié)果:

union(self, *args, **kwargs):

說明:并集,創(chuàng)建一個新的 set 集合,即新建的 set 集合包含兩集合的所有元素。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,22,44,])

# 并集,包含s1 和 s2 的所有元素
ret = s1.union(s2)
print(ret)

運行結(jié)果:

update(self, *args, **kwargs):

說明:更新,即將傳入集合并入到原集合中。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
s1 = set([11,22,33])
s2 = set([11,22,44])

# 更新,將 s2 并入到 s1 中
s1.update(s2)
print(s1)

# 上面?zhèn)鞯氖莻€集合,也是可以傳列表的
# 本質(zhì)上是循環(huán)里面所有元素,將其添加到集合里面
s1.update([44,55,66])
print(s1)

運行結(jié)果:

17.3-練習(xí)題:

尋找差異:

條件:

# 數(shù)據(jù)庫中原有
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 新匯報的數(shù)據(jù)
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 }
}
    
需要更新:? 注意:無需考慮內(nèi)部元素是否改變,只要原來存在,新匯報也存在,就是需要更新
需要刪除:?
需要新建:?
第一種方式:
#!/usr/bin/env 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 }
}

# cmdb 新匯報的數(shù)據(jù)
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 }
}

"""
先獲取到的是可迭代的,在 2.7 中獲取的是列表,在 3.x 中是另一種特殊的數(shù)據(jù)結(jié)構(gòu),但這個數(shù)據(jù)結(jié)構(gòu)是可以被循環(huán)的.
由于先獲取到的不是 set 集合,不能使用其內(nèi)置方法,所以需轉(zhuǎn)換成 set
"""
old_keys = set(old_dict.keys())
new_keys = set(new_dict.keys())

# 取交集,兩集合的公共元素
update = old_keys.intersection(new_keys)
print("需要更新的:",update)

# 原集合中存在,但傳入集合不存在的元素
delete = old_keys.difference(new_dict)
print("需刪除的:",delete)

new = new_keys.difference(old_keys)
print("需要新建的::",new)

運行結(jié)果:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容