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)
dic2 =
.fromkeys(lst,[1
,2])
dic2[
"a"]3)
print
(dic2)
dic3 =
dic3[
"a"]=[
]dic3[
"b"]=[
]dic3[
"a"]1)
print
(dic3)
dic =
#pop() 通過鍵去刪除鍵值對 (若沒有該鍵可設定預設值,預防報錯)
res = dic.pop(
"bottom"
)print
(res)
print
(dic)
### 可以在pop當中設定預設值,預防報錯
res = dic.pop(
"bottom123"
,"沒有"
)print
(res)
#popitem() 刪除最後乙個鍵值對
res = dic.popitem(
)print
(dic)
print
(res)
#clear() 清空字典
dic.clear(
)print
(dic)
批量更新(有該鍵就更新,沒該鍵就新增)
"""有該鍵就更新,沒該鍵就新增"""
dic =
newdic =
dic.update(newdic)
print
(dic)
dic.update(hj=
"未來戰士"
,wyq=
"警察"
)print
(dic)
#get() 通過鍵獲取值(若沒有該鍵可設定預設值,預防報錯)
dic =
# res = dic["www"]
# get方法可以預防報錯,預設返回none
res = dic.get(
"www"
)# get方法可以設定預設值
res = dic.get(
"www"
,"該鍵不存在"
)print
(res)
可迭代物件: 容器型別資料 ,
range物件 , 迭代器
dic =
res = dic.keys(
)print
(res)
res = dic.values(
)print
(res)
res = dic.items(
)print
(res)
for k,v in res:
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 = set1.issubset(set2)
res = set2.issubset(set1)
print
(res)
res = set1 < set2
print
(res)
res = set1.issuperset(set2)
print
(res)
res = set1 > set2
print
(res)
res = set1.isdisjoint(set2)
print
(res)
setvar =
#add() 向集合中新增資料 (一次加乙個)
setvar.add(
"朱佳怡"
)print
(setvar)
#update() 迭代著增加 (一次加一堆)
lst =
["a"
,"b"
,"c123"
]setvar.update(lst)
print
(setvar)
strvar =
"abc"
setvar =
set(
)setvar.update(
"abc"
)print
(setvar)
setvar =
#clear() 清空集合
setvar.clear(
)print
(setvar)
res = setvar.pop(
)print
(res)
setvar.discard(
"a")
print
(setvar)
setvar.remove(
"a")
print
(setvar)
setvar.remove(
"aaa"
)print
(setvar)
特徵:冰凍集合一旦建立,不能再做任何的新增或者刪除操作,只能做交叉並補
fz =
frozenset()
print
(fz ,
type
(fz)
)lst =
["a"
,"b"
,"c"
]fz =
frozenset
(lst)
print
(fz,
type
(fz)
)
for i in fz:
print
(i)
fz1 =
frozenset([
"a",
"b",
"c"]
)fz2 =
frozenset([
"a",
"b",
"c",
"d"]
)res = fz1 & fz2
res = fz2 - fz1
print
(res)
fz1.add(
"f") error
字典相關函式 集合相關的操作 交差並補
dic dic top 廉頗 dic middle 妲己 dic bottom 孫尚香 print dic 1.fromkeys 使用一組鍵和預設值建立字典 快速定義字典 lst a b c fromkeys 塞滿鍵的容器,預設值 dic fromkeys lst,none print dic 1....
字典相關函式
增刪改查 增 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 ...
字典和集合
字典 dict 字典 dict是一種無序的可變容器,序列的索引對它失效,它的索引是自定義的 形式 d d d dict a 1,b 2 d dict name libai age 30 d dict.fromkeys 迭代物件 將乙個迭代物件的每個元素作為鍵,值預設全部為none 字典方法 例 d ...