[英]Convert OrderedDict to normal dict
我有一個這樣的 OrderedDicts 列表:
input = [OrderedDict([('id', 'mp-1'), ('name', 'John')]), OrderedDict([('id', 'mp-2'), ('name', 'Peter')])]
我怎樣才能把它轉換成這樣的普通字典列表
output = [{'id': 'mp-1', 'name': 'John'}, {'id': 'mp-2', 'name': 'Peter'}]
您可以使用dict
構造函數:
>>> output = [dict(i) for i in input]
>>> output
[{'id': 'mp-1', 'name': 'John'}, {'id': 'mp-2', 'name': 'Peter'}]
用這個:
output = []
for odict in input:
output.append(dict(odict))
它的速度並不快,但它的可讀性和足夠好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.