
高版本我一直使用的是3.8的版本,我先用python3.8的版本來測驗查看是不是會產生有序的字......
【閱讀全文】
test_dict = {'o': 1,'p': 2,'q': 3,'r': 4,'s': 5,'t': 6}
使用ksys()函式驗證字典的鍵是否有序
print(test_dict.keys())
# dict_keys(['o', 'p', 'q', 'r', 's', 't'])
# Process finished with exit code 0
遍歷字典再次驗證
for key,value in test_dict.items():
print(key,value)
# dict_keys(['o', 'p', 'q', 'r', 's', 't'])
# o 1
# p 2
# q 3
# r 4
# s 5
# t 6
發現python3.8版本的字典集合真的變成有序字典了,最后,找個3.6以下的版本再來驗證一番,使用同樣的資料來進行驗證
# Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
# Type "help", "copyright", "credits" or "license" for more information.
# >>> test_dict = {'o': 1,'p': 2,'q': 3,'r': 4,'s': 5,'t': 6}
# >>> print(test_dict.keys())
# ['o', 'q', 'p', 's', 'r', 't']
# >>>
首先keys()函式遍歷的鍵就是無序的
# >>> for key,value in test_dict.items():
# ... print(key,value)
# ...
# ('o', 1)
# ('q', 3)
# ('p', 2)
# ('s', 5)
# ('r', 4)
# ('t', 6)
最后,遍歷的鍵值都是無序的,今天就到這里了,小編才加完班該回家了!

【往期精彩】
這么多的內置函式能記住嗎?對python的68個內置函式分類總結!
必須要會的檔案操作物件File,python檔案讀寫操作利器!
你不知道的CS模式的行程管理工具,狀態監測、專案啟停一目了然!
如何將一個python應用以docker鏡像的方式來運行?
python超贊插件you-get,執行一行命令即可下載、命令列下載工具推薦!
歡迎關注作者公眾號【Python 集中營】,專注于后端編程,每天更新技術干貨,不定時分享各類資料!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/341706.html
標籤:Python
