Python字典的key與value命名規則

2021-09-27 02:04:54 字數 792 閱讀 7740

乙個鍵對應乙個值,不允許乙個鍵對應多個值,但可以多個鍵對應乙個值

變數名=

#字典建立    無序  key不能變   list列表不能做key,元組可以

phonenumber=

#乙個key對應乙個值,不允許乙個key對應多個值,key不能重複

test=

print

("a's phonenumber is "

+str

(phonenumber[

"a"]))

#修改值

test[

"pi"]=

0.618

print

(str

(test[

"pi"])

)#新增 當字典中沒有key時,會自動新增

test[

"b"]=98

print

(str

(test[

"b"]))

#輸出字典

print

(str

(test)

)#刪除元素

del test[

"pi"

]print

(str

(test)

)#清空字典

test.clear(

)print

(str

(test)

)#刪除字典

del test

#print("test:"+str(test))#報錯,因為沒有這個字典了

python字典交換key與value的兩種方法

交換key與value的兩種方法 利用jupyter notebook操作 方法一 使用dict.items 方法 dict ori dict new print dict new 執行結果 方法二 使用zip 方法 dict ori dict new2 dict zip dict ori.valu...

python 字典key值報錯

報錯 typeerror unhashable type set 或 typeerror unhashable type list 原因 python的字典型別的key不支援set或list,set裡面的物件是hash儲存,如果儲存乙個list物件,而後改變了list物件,那set中剛才儲存的值的h...

python禁止字典key排序

import collections data collections.ordereddict data b 3 data a 1 data jsonify d return make response data,200 1.正常的python dict是按字母順序排序的,所以要使用orderedd...