如何将嵌套字典转换为具有关键命令的列表 [英] How to convert nested dictionary to list with key orders

查看:123
本文介绍了如何将嵌套字典转换为具有关键命令的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从嵌套字典中列出一个列表:

I want to make a list out of the nested dictionary:

 {'Name': {'20': 'Paul Merrill', '21': 'Brynne S. Barr', }, 
 'Phone': {'20': '1-313-739-3854', '21': '939-4818', }, 
 'Address': {'20': '916-8087 Vehicula Rd.', '21': '878-2231 Suspendisse Rd.', },
 'City': {'20': 'Le Mans', '21': 'Wilhelmshaven',}

以'20'作为标识符的列表,因此将如下所示:

to a list with '20' as the identifier, so it will be something like this:

['20', 'Paul Merril', '1-313-739-3854', '916-8087 Vehicula Rd.', 'Le Mans']

我尝试使用值和键选项,但它们似乎不起作用. 有人可以帮我吗?

I have tried to use value and key options, but they don't seem to work. Could someone help me with this?

推荐答案

使用或者@Henry Yik的解决方案,谢谢:

Or solution from @Henry Yik, thank you:

L = [v.get("20") for v in d.values()]
print (L)
['Paul Merrill', '1-313-739-3854', '916-8087 Vehicula Rd.', 'Le Mans']

如果还需要添加20:

L = ['20'] + L

或者:

L = ['20', *L]

print (L)
['20', 'Paul Merrill', '1-313-739-3854', '916-8087 Vehicula Rd.', 'Le Mans']

这篇关于如何将嵌套字典转换为具有关键命令的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆