获取嵌套字典的所有键 [英] Get all keys of a nested dictionary

查看:110
本文介绍了获取嵌套字典的所有键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,当前仅打印初始字典的值.但是,我想遍历嵌套字典的每个键,以便最初只打印名称.请在下面查看我的代码:

I have the below code which currently just prints the values of the initial dictionary. However I would like to iterate through every key of the nested dictionary to initially just print the names. Please see my code below:

Liverpool = {
    'Keepers':{'Loris Karius':1,'Simon Mignolet':2,'Alex Manninger':3},
    'Defenders':{'Nathaniel Clyne':3,'Dejan Lovren':4,'Joel Matip':5,'Alberto Moreno':6,'Ragnar Klavan':7,'Joe Gomez':8,'Mamadou Sakho':9}
}

for k,v in Liverpool.items():
    if k =='Defenders':
       print(v)

推荐答案

以下是可打印所有团队成员的代码:

Here is code that would print all team members:

for k, v in Liverpool.items():
    for k1, v1 in v.items():
        print(k1)

因此,您只需一个一个地迭代每个内部字典并打印值即可.

So you just iterate every inner dictionary one by one and print values.

这篇关于获取嵌套字典的所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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