【问题标题】:converting an ordered dictionary into normal dictionary in python [closed]在python中将有序字典转换为普通字典[关闭]
【发布时间】:2015-04-04 19:50:51
【问题描述】:

我有一个有序字典,其中包含一个有序字典作为值,例如:

OrderedDict([(u'catalog', OrderedDict([(u'book', [OrderedDict([(u'@id', u'bk101'), (u'author', OrderedDict([(u'@a_id', u'a101'), (u'first', u'Gambardella'), (u'last', u'Matthew'), (u'email', u'matthew@standford.org')])), (u'title', u"XML Developer's Guide"), (u'genre', u'Computer'), (u'price', u'44.95'), (u'publish_date', u'2000-10-01'), (u'description', u'An in-depth look at creating applications \n      with XML.')]), OrderedDict([(u'@id', u'bk102'), (u'author', u'Ralls, Kim'), (u'title', u'Midnight Rain'), (u'genre', u'Fantasy'), (u'price', u'5.95'), (u'publish_date', u'2000-12-16'), (u'description', u'A former architect battles corporate zombies, \n      an evil sorceress, and her own childhood to become queen \n      of the world.')]), OrderedDict([(u'@id', u'bk103'), (u'author', u'Corets, Eva'), (u'title', u'Maeve Ascendant'), (u'genre', u'Fantasy'), (u'price', u'5.95'), (u'publish_date', u'2000-11-17'), (u'description', u'After the collapse of a nanotechnology \n      society in England, the young survivors lay the \n      foundation for a new society.')]), OrderedDict([(u'@id', u'bk104'), (u'author', u'Corets, Eva'), (u'title', u"Oberon's Legacy"), (u'genre', u'Fantasy'), (u'price', u'5.95'), (u'publish_date', u'2001-03-10'), (u'description', u'In post-apocalypse England, the mysterious \n      agent known only as Oberon helps to create a new life \n      for the inhabitants of London. Sequel to Maeve \n      Ascendant.')]), OrderedDict([(u'@id', u'bk105'), (u'author', u'Corets, Eva'), (u'title', u'The Sundered Grail'), (u'genre', u'Fantasy'), (u'price', u'5.95'), (u'publish_date', u'2001-09-10'), (u'description', u"The two daughters of Maeve, half-sisters, \n      battle one another for control of England. Sequel to \n      Oberon's Legacy.")]), OrderedDict([(u'@id', u'bk106'), (u'author', u'Randall, Cynthia'), (u'title', u'Lover Birds'), (u'genre', u'Romance'), (u'price', u'4.95'), (u'publish_date', u'2000-09-02'), (u'description', u'When Carla meets Paul at an ornithology \n      conference, tempers fly as feathers get ruffled.')]), OrderedDict([(u'@id', u'bk107'), (u'author', u'Thurman, Paula'), (u'title', u'Splish Splash'), (u'genre', u'Romance'), (u'price', u'4.95'), (u'publish_date', u'2000-11-02'), (u'description', u'A deep sea diver finds true love twenty \n      thousand leagues beneath the sea.')]), OrderedDict([(u'@id', u'bk108'), (u'author', u'Knorr, Stefan'), (u'title', u'Creepy Crawlies'), (u'genre', u'Horror'), (u'price', u'4.95'), (u'publish_date', u'2000-12-06'), (u'description', u'An anthology of horror stories about roaches,\n      centipedes, scorpions  and other insects.')]), OrderedDict([(u'@id', u'bk109'), (u'author', u'Kress, Peter'), (u'title', u'Paradox Lost'), (u'genre', u'Science Fiction'), (u'price', u'6.95'), (u'publish_date', u'2000-11-02'), (u'description', u'After an inadvertant trip through a Heisenberg\n      Uncertainty Device, James Salway discovers the problems \n      of being quantum.')]), OrderedDict([(u'@id', u'bk110'), (u'author', u"O'Brien, Tim"), (u'title', u'Microsoft .NET: The Programming Bible'), (u'genre', u'Computer'), (u'price', u'36.95'), (u'publish_date', u'2000-12-09'), (u'description', u"Microsoft's .NET initiative is explored in \n      detail in this deep programmer's reference.")]), OrderedDict([(u'@id', u'bk111'), (u'author', u"O'Brien, Tim"), (u'title', u'MSXML3: A Comprehensive Guide'), (u'genre', u'Computer'), (u'price', u'36.95'), (u'publish_date', u'2000-12-01'), (u'description', u'The Microsoft MSXML3 parser is covered in \n      detail, with attention to XML DOM interfaces, XSLT processing, \n      SAX and more.')]), OrderedDict([(u'@id', u'bk112'), (u'author', OrderedDict([(u'@a_id', u'a007'), (u'first', u'Galos'), (u'last', u'Mike'), (u'email', u'mike@gmail.com')])), (u'title', u'Visual Studio 7: A Comprehensive Guide'), (u'genre', u'Computer'), (u'price', u'49.95'), (u'publish_date', u'2001-04-16'), (u'description', u'Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n      integrated into a comprehensive development \n      environment.')])])]))])

我想遍历这个字典,想把它转换成普通字典。

这怎么可能……

【问题讨论】:

  • 您有什么特别的理由要这样做吗? OrderedDict 也可以像普通字典一样工作,只是它的键是有序的。
  • 您是否希望“扁平化”您拥有的嵌套结构?或者只是将OrderedDict 的每个实例转换为dict?
  • 我正在寻找使用给定结构迭代地转换每个实例..
  • 您可以调用dict 将某些内容投射到字典中。将它与条件 isinstance(x, OrderedDict) 检查和递归结合起来,然后你就完成了。

标签: python dictionary


【解决方案1】:

正如 cmets 中的 mu 所指出的,为什么要这样做并不是很清楚。 OrderedDicts 就像字典一样,只有极少数例外。

但是对于您的特定输入,您可以通过以下方式完成此操作:

from collections import OrderedDict
d1 = OrderedDict(<snip>)

def convert(e):
    if isinstance(e, (dict, OrderedDict)):
        return {k:convert(v) for k,v in e.iteritems()}
    if isinstance(e, list):
        return [convert(v) for v in e]
    return e

d2 = convert(d1)

import pprint
pprint.pprint(d2)

输出的“头”(pprint.pprint(d2))看起来像:

{u'目录': {u'book': [{u'@id': u'bk101', u'作者':{u'@a_id':u'a101', u'email': u'matthew@standford.org', u'first': u'Gambardella', u'last': u'Matthew'}, u'description': u'深入了解使用 XML 创建应用程序\n。', u'genre': u'电脑', u'price': u'44.95', u'publish_date': u'2000-10-01', u'title': u"XML 开发者指南"}, {u'@id': u'bk102', u'author': u'Ralls, Kim', u'description': u'一位前建筑师与企业僵尸、一个邪恶的女巫\n和她自己的童年作斗争,成为世界的女王\n。', 你的“类型”:你的“幻想”, u'price': u'5.95', u'publish_date': u'2000-12-16', u'title': u'午夜雨'},

【讨论】:

  • 其实,我已经使用xmltodict包将xml文件转换为dict,内容类似于Gambardella Matthew matthew@standford.orgXML 开发人员指南 计算机44.952000-10-01深入了解使用 XML 创建应用程序。目录>
猜你喜欢
  • 2012-04-22
  • 2020-08-24
  • 2023-03-07
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2016-10-16
相关资源
最近更新 更多