【发布时间】: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