我有这样的嵌套字典
profile = {
"Person":{
"name":{
"First_Name":["John"],
"Last_Name":['Doe']
}
},
"Object":{
"name":{
"First_Name":['John'],
"Last_Name":['Doe']
}
}
}
我不知道如何编写一段代码来打印查找“ First_Name”和“ John”并确定它是键还是值的步骤。嵌套字典中可能还存在多个相同的值,而我想要所有这些值。例如:
First_Name is a key and is located in profile['Person']['name']['First_Name']
John is a value and is located in profile['Person']['name']['First_Name']
First_Name is a key and is located in profile['Object']['name']['First_Name']
John is a value and is located in profile['Object']['name']['First_Name']
答案 0 :(得分:1)
这个问题有点含糊,但是这样的解决方案可能行得通。
此解决方案将为所有未嵌套dict
的值打印输出样式。如果键的值为dict
类型,则该函数将递归直到找到未嵌套打印的值。
def print_nested_dict(nested_dict, name, prior_keys=[]):
for key, value in nested_dict.items():
# current_key_path is a list of each key we used to get here
current_key_path = prior_keys + [key]
# Convert that key path to a string
key_path_str = ''.join('[\'{}\']'.format(key) for key in current_key_path)
# If the value is a dict then recurse
if isinstance(value, dict):
print_nested_dict(value, name, current_key_path)
else:
# Else lets print the key and value for this value
print("{} is a key and is located in {}{}".format(key, name, key_path_str))
print("{} is a value and is located in {}{}".format(value, name, key_path_str))
print_nested_dict(profile, "profile")
输出:
First_Name is a key and is located in profile['Person']['name']['First_Name']
['John'] is a value and is located in profile['Person']['name']['First_Name']
Last_Name is a key and is located in profile['Person']['name']['Last_Name']
['Doe'] is a value and is located in profile['Person']['name']['Last_Name']
First_Name is a key and is located in profile['Object']['name']['First_Name']
['John'] is a value and is located in profile['Object']['name']['First_Name']
Last_Name is a key and is located in profile['Object']['name']['Last_Name']
['Doe'] is a value and is located in profile['Object']['name']['Last_Name']
答案 1 :(得分:1)
您可以尝试这样。
建议:创建一个功能并实现可重用性(功能方法),这是最好的方法(您也可以使用 OOP 方法)。在这里,我只是试图满足需求。
如果以后选择 OOP ,可以稍微看一下https://stackoverflow.com/a/55671535/6615163并尝试一下(如果您不熟悉 OOP ,否则没关系)。
在这里,我尝试添加
Last_Name
(即所有键),如果只想使用First_Name
,则可以在inner
(第3个)循环内放置条件语句并停止跳过添加到列表中的内容。
import json
profile = {
"Person":{
"name":{
"First_Name":["John"],
"Last_Name":['Doe']
}
},
"Object":{
"name":{
"First_Name":['John'],
"Last_Name":['Doe']
}
}
}
# START
messages = []
for key1 in profile:
for key2 in profile[key1]:
for key3 in profile[key1][key2]:
message = "{0} is a {1} and is located in profile['{2}']['{3}']['{4}']"
messages.append(message.format(key3, 'key', key1, key2, key3))
messages.append(message.format(profile[key1][key2][key3][0], 'value', key1, key2, key3))
# --- Pretty print the list `messages` (indentation 4) ---
print(json.dumps(messages, indent=4))
# [
# "First_Name is a key and is located in profile['Person']['name']['First_Name']",
# "John is a value and is located in profile['Person']['name']['First_Name']",
# "Last_Name is a key and is located in profile['Person']['name']['Last_Name']",
# "Doe is a value and is located in profile['Person']['name']['Last_Name']",
# "First_Name is a key and is located in profile['Object']['name']['First_Name']",
# "John is a value and is located in profile['Object']['name']['First_Name']",
# "Last_Name is a key and is located in profile['Object']['name']['Last_Name']",
# "Doe is a value and is located in profile['Object']['name']['Last_Name']"
# ]
# --- As a string ---
print('\n'.join(messages))
# First_Name is a key and is located in profile['Person']['name']['First_Name']
# John is a value and is located in profile['Person']['name']['First_Name']
# Last_Name is a key and is located in profile['Person']['name']['Last_Name']
# Doe is a value and is located in profile['Person']['name']['Last_Name']
# First_Name is a key and is located in profile['Object']['name']['First_Name']
# John is a value and is located in profile['Object']['name']['First_Name']
# Last_Name is a key and is located in profile['Object']['name']['Last_Name']
# Doe is a value and is located in profile['Object']['name']['Last_Name']