📜  Python – 如何迭代嵌套字典?

📅  最后修改于: 2022-05-13 01:55:39.571000             🧑  作者: Mango

Python – 如何迭代嵌套字典?

在本文中,我们将讨论如何在Python中迭代嵌套字典。

嵌套字典意味着字典中的字典,我们将看到迭代这种数据结构的所有可能方式。

正在使用的嵌套字典:

为此,我们将使用 for 循环遍历字典以获取嵌套字典的所有键值。

句法:

for i in dictionary_name:
     print(dictionary_name[i])

在哪里

  • dictionary_name 是输入字典
  • dictionary_name[i] 是获取字典的值

示例:从字典中获取嵌套字典的Python程序

Python3
# create a nested dictionary with 3 
# fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
  
  
# iterate all the nested dictionaries with 
# both keys and values
for i in data:
     
    # display
    print(data[i])


Python3
# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
  
  
# iterate all the nested dictionaries with keys
for i in data:
  # display
    print(data[i].keys())


Python3
# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
  
  
# iterate all the nested dictionaries with values
for i in data:
  # display
    print(data[i].values())


输出:

{'Name': 'Bobby', 'Id': 1, 'Age': 20}
{'Name': 'ojaswi', 'Id': 2, 'Age': 22}
{'Name': 'rohith', 'Id': 3, 'Age': 20}

如果这是需求所要求的,也可以只获取键或值。再次为此 for 循环使用了一些变化。

为了从每次迭代的嵌套字典中获取键,调用 keys()函数。

句法:

data[i].keys()

示例:从嵌套字典中获取键的Python程序

蟒蛇3

# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
  
  
# iterate all the nested dictionaries with keys
for i in data:
  # display
    print(data[i].keys())

输出:

dict_values(['Bobby', 1, 20])
dict_values(['ojaswi', 2, 22])
dict_values(['rohith', 3, 20])

与获取值类似,在每次迭代后 values()函数用于完成工作。

句法:

data[i].values()

示例:从嵌套字典中获取值的Python程序

蟒蛇3

# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
  
  
# iterate all the nested dictionaries with values
for i in data:
  # display
    print(data[i].values())

输出:

dict_values(['Bobby', 1, 20])
dict_values(['ojaswi', 2, 22])
dict_values(['rohith', 3, 20])