我有一个有序的字典,其中包含一个有序的字典,如:
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.')])])]))])
我想迭代这本字典,并希望将其转换为普通字典。
这是怎么可能的......
答案 0 :(得分:2)
正如评论中mu所说,为什么你想要这样做并不是很清楚。 OrderedDict
就像字典一样,只有极少数例外。
但是对于您的具体输入,您可以通过以下方式完成此任务:
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'catalog': {u'book': [{u'@id': u'bk101', u'author': {u'@a_id': u'a101', u'email': u'matthew@standford.org', u'first': u'Gambardella', u'last': u'Matthew'}, u'description': u'An in-depth look at creating applications \n with XML.', u'genre': u'Computer', u'price': u'44.95', u'publish_date': u'2000-10-01', u'title': u"XML Developer's Guide"}, {u'@id': u'bk102', u'author': u'Ralls, Kim', u'description': u'A former architect battles corporate zombies, \n an evil sorceress, and her own childhood to become queen \n of the world.', u'genre': u'Fantasy', u'price': u'5.95', u'publish_date': u'2000-12-16', u'title': u'Midnight Rain'},