如何从OrderedDict转换
OrderedDict([(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')])
到列表?
[(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')]
THX !!
答案 0 :(得分:30)
在Python 2.x上,只需使用items
方法:
>>> from collections import OrderedDict
>>> dct = OrderedDict([(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')])
>>> dct.items()
[(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')]
>>>
在Python 3.x上,执行相同操作,但也将其放在list
:
>>> from collections import OrderedDict
>>> dct = OrderedDict([(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')])
>>> list(dct.items())
[(73, 'Mocha My Day'), (77, 'Coffee Cafe'), (83, 'Flavour Fusion'), (85, 'Mexican Grill')]
>>>