本文介紹了如何在 python3 中將 OrderedDict 轉換為常規字典的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在努力解決以下問題:我想像這樣轉換 OrderedDict
:
I am struggling with the following problem:
I want to convert an OrderedDict
like this:
OrderedDict([('method', 'constant'), ('data', '1.225')])
到這樣的常規字典中:
{'method': 'constant', 'data':1.225}
因為我必須將它作為字符串存儲在數據庫中.轉換后,順序不再重要,所以我無論如何都可以保留有序功能.
because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordered feature anyway.
感謝任何提示或解決方案,
Thanks for any hint or solutions,
本
推薦答案
>>> from collections import OrderedDict
>>> OrderedDict([('method', 'constant'), ('data', '1.225')])
OrderedDict([('method', 'constant'), ('data', '1.225')])
>>> dict(OrderedDict([('method', 'constant'), ('data', '1.225')]))
{'data': '1.225', 'method': 'constant'}
>>>
但是,要將其存儲在數據庫中,最好將其轉換為 JSON 或 Pickle 等格式.使用 Pickle,您甚至可以保持訂單!
However, to store it in a database it'd be much better to convert it to a format such as JSON or Pickle. With Pickle you even preserve the order!
這篇關于如何在 python3 中將 OrderedDict 轉換為常規字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!