字典相關函式 集合相關的操作 交差並補

2022-06-08 11:39:10 字數 3905 閱讀 2474

dic ={}

dic[

"top

"] = "廉頗"

dic[

"middle

"] = "妲己"

dic[

"bottom

"] = "

孫尚香"

print(dic)

1.fromkeys()  使用一組鍵和預設值建立字典 (快速定義字典)

lst = ["

a","

b",'c'

]#fromkeys(塞滿鍵的容器,預設值)

dic ={}.fromkeys(lst,none)

print(dic) #

1.pop()       通過鍵去刪除鍵值對 (若沒有該鍵可設定預設值,預防報錯)

dic = 

#指定鍵刪除對應的鍵值對

res = dic.pop("

jungle")

#若沒有該鍵可設定預設值,預防報錯

res = dic.pop("

middle123434

","抱歉,該鍵不存在")

print

(res)

print(dic)

2.popitem()   刪除最後乙個鍵值對 

dic = 

res =dic.popitem()

print

(res)

print(dic)

3.clear()  清空字典

dic.clear()

print(dic)

update() 批量更新(有該鍵就更新,沒該鍵就新增)

dic =  

#沒該鍵就新增

dic_new =

dic.update(dic_new)

print

(dic)

#有該鍵就更新

dic_new =

dic.update(dic_new)

print

(dic)

#額外的用法(了解)

dic.update(middle="

甄姬",jungle="劉備"

)print(dic)

get()    通過鍵獲取值(若沒有該鍵可設定預設值,預防報錯)

dic =  

res = dic.get("

top123")

res = dic.get("

top123

","抱歉,改鍵不存在")

#res = dic["top"]

print(res)

#

keys() 將字典的鍵組成新的可迭代物件

dic =

res =dic.keys()

print

(res , type(res))

#values() 將字典中的值組成新的可迭代物件

res =dic.values()

print

(res , type(res))

#items() 將字典的鍵值對湊成乙個個元組,組成新的可迭代物件

res =dic.items()

print

(res , type(res))

"""dict_items(

[ ('top', '廉頗'),

('middle', '妲己'),

('bottom', '孫尚香')]

) """

for k,v in

dic.items():

print(k,v)

set1 =

set2 =

res =set1.intersection(set2)

print

(res)

#簡寫

res = set1 &set2

print(res)

res =set1.difference(set2)

print

(res)

#簡寫

res = set1 -set2

print(res)

res =set1.union(set2)

print

(res)

#簡寫

res = set1 |set2

print(res)

res =set1.symmetric_difference(set2)

print

(res)

#簡寫

res = set1 ^set2

print(res)

set1 =

set2 =

res =set2.issubset(set1)

print(res)

# 簡寫

res = set2 < set1

print(res)

res =set1.issuperset(set2)

print

(res)#簡寫

res = set1 >set2

print(res)

set1 = 

set2 =

res =set1.isdisjoint(set2)

print(res)

setvar = 

#1.一次加乙個

#add() 向集合中新增資料

setvar.add("

周星馳"

)setvar.add(

"周樹人")

print

(setvar)

#2.一次加一堆

#update() 迭代著增加

setvar =

lst = ["

馬蓉","

馬化騰","

馬戶","

馬雲","碼農"

]lst = "

abc"

setvar.update(lst)

print(setvar)

setvar =

1clear()   清空集合

#

setvar.clear()

#print(setvar)

2. pop()     隨機刪除集合中的乙個資料

#

res = setvar.pop()

#print(res)

#print(setvar)

3. discard() 刪除集合中指定的值(不存在的不刪除 推薦使用)

setvar.discard("

郭傑瑞"

)setvar.discard(

"郭傑瑞11122333")

print(setvar)

"""

冰凍集合一旦建立,不能在進行任何修改,只能做交叉並補操作

"""#

frozenset 可強轉容器型別資料變為冰凍集合

lst = ["

馬蓉","

馬化騰","

馬戶","

馬雲","碼農"

]fz =frozenset(lst)

print

(fz,type(fz))

fz1 = frozenset( )

fz2 = frozenset( )

#只能做交叉並補操作

res = fz1 &fz2

print

(res)

#可以做新增操作麼? 不可以

fz1.add("

abc")

字典和集合的相關函式

dic dic top 南韓the shy dic middle 肉雞rookie dic bottom a water dic jungle 鞋王 dic support bao blue print dic lst a b c dic fromkeys lst,none print dic di...

字典相關函式

增刪改查 增 fromkeys dic dic dd 你好 dic ss lili dic ww wangwei dic qq ashui dic support 寶藍 print dic dic.pop nj print dic dic res dic.pop q qq 沒有 print res ...

集合的相關操作

include using namespace std typedef struct nodenode,linklist define size sizeof node define false 0 define true 1 初始化集合 void initlinklist linklist hea...