3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
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')] 如果有需要寫大量更換帳號、密碼程式需求,剛好可以使用這種方法來解決問題 例如: acc_pas = {"acc_1":"pasd_1","acc_2":"pasd_2","acc_3":"pasd_3","acc_4":"pasd_4"} a = acc_pas.items() for acc,pas in a: print("帳號-->",acc) print("密碼-->",pas) 輸出結果 建立 dictonary 方法很多,而以下兩種方法都可以達到我們想要的結果 languages = { 'name': 'python', 'version': '3.5'}dict([('name', 'python'), ('version', '3.5')])

本文由python-learnnotebookblogspotcom提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦