是否有可能使用collections.OrderedDict
就像正常的字典句法,某種from __???__ import ???
派生物,而不轉換現有的字典使用[(key, value), ..., ]
?我有幾個嵌入式json的代碼,並希望保持秩序。collections.OrderedDict正常字典的語法
回答
根本沒有collections.OrderedDict
就可以有一個有序字典:只需升級到Python 3.6。
注意:在Python 3.6字典是保序。這實際上消除了性能問題。
只需使用常規詞典。
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> {1:1, 2:2}
{1: 1, 2: 2}
>>> {2:2, 1:1}
{1: 1, 2: 2}
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> {1:1, 2:2}
{1: 1, 2: 2}
>>> {2:2, 1:1}
{2: 2, 1: 1}
好的建議,考慮到升級不會破壞他們的代碼 –
https://docs.python.org/3.6/whatsnew/3.6.html#pep-520-preserving-class-attribute-definition-order指出:'訂單保存這個新實現的方面被認爲是一個實現細節,不應該依賴'。 –
@hiroprotagonist - 奇怪的是,PEP只是聲明'dict'現在是有序的,但是PEP的總結表明它是一個實現細節。 – TigerhawkT3
- 1. 函數字典的語法?
- 2. 使用collections.OrderedDict是不好的做法嗎?
- 3. 字典對象語法?
- 4. 將瑞典語字符串轉換爲貨幣無法正常轉換
- 5. HTML&Javascript - 簡體英語 - 法語字典
- 6. Swift什麼是正確的字典語法?
- 7. 具有多個鍵的字典無法正常工作
- 8. 巨大的字典Python無法正常工作
- 9. Python的有序字典常規字典
- 10. YAML中空字典的語法
- 11. TAL嵌套的字典語法
- 12. 字典值中字典的正確方法?
- 13. Apache NiFi - ScanContent字典格式無法正常工作
- 14. 迭代通過字典無法正常工作
- 15. GWT CSS語法無法正常工作
- 16. 使用字典的正確方法
- 17. 俄語 - 英語Multitran字典
- 18. 調試「不正確的語法」異常
- 19. jQuery的語句()無法正常工作
- 20. C++/CLI通用::字典聲明語法
- 21. 語法錯誤列表/字典
- 22. 語法錯誤:在創建字典
- 23. 創建Python字典:語法問題
- 24. 字典KeyError異常
- 25. 的Javascript switch語句無法正常運行與字符串
- 26. NHibernate異常:關鍵字'用戶'附近的語法不正確
- 27. 字典()的Python外語
- 28. JavaScript的語言字典
- 29. asp經典的vbscript語法
- 30. 訪問trackMetadata字典的SPTAudioStreamingController中的常量鍵的正確方法?
我想從正常的字典語法構造collections.OrderedDict。也就是說,'x = {「A」:1,「B」:2};斷言類型(x)== collections.OrderedDict'。當然,第二個是False。那麼,是否有可能通過某種有點冒險的衍生物來實現它? – xosp7tom
使用某種類型的文字語法將無助於解碼JSON;只需告訴'json.loads()'代碼在解碼時使用'OrderedDict'。看到重複。 –