python dictionary 常用指令教學





dict.values()

如果要確認 dictionary 到底還有哪些 value 的時候,可以使用此function 來做 check
dict = { 1 : 2, 'a' : 'b', 'hello' : 'world'

輸出結果 ['b', 2, 'world']


dict.keys()

可用keys() function 來去檢查所有的key,不需要把全部的結果都印出來
dict = { 1 : 2, 'a' : 'b''hello' : 'world' } 

輸出結果
['a', 1, 'hello']


dict.items()

items()會返回一整個 list,並且是組一組的 dictionary 所組成。
輸出結果
[('a', 'b'), (1, 2), ('hello', 'world')]

如果有需要寫大量更換帳號、密碼程式需求,剛好可以使用這種方法來解決問題

例如:



輸出結果




建立 dictonary 方法很多,而以下兩種方法都可以達到我們想要的結果

languages = {
'name': 'python',
'version': '3.5'
}

dict([('name', 'python'), ('version', '3.5')])

沒有留言

技術提供:Blogger.