我知道有关访问密钥的问题,之前已经询问过嵌套字典中的值,但是我对以下代码中遇到了一些问题:
用于访问嵌套字典的键和值,如下所示:
example_dict = {'key_outer_01': {'key_inner_01': 'value_inner_01'},
'key_outer_02': {'key_inner_02': 'value_inner_02'}}
我有以下代码:
def get_key_value_of_nested_dict(nested_dict):
for key, value in nested_dict.items():
outer_key = None
inner_key = None
inner_value = None
if isinstance(value, dict):
outer_key = key
get_key_value_of_nested_dict(value)
else:
inner_key = key
inner_value = value
return outer_key, inner_key, inner_value
我得到的输出是:
key_outer_01 None None
我在这里做错了什么?
答案 0 :(得分:1)
在递归调用中,您将 outer_key , inner_key 和 inner_value 设置为None
。但在if isintance(value, dict)
中,您只是将 outer_key 重新定义为key
。您可能希望分配inner_key
和inner_value
的新值。
为inner_key
和inner_value
分配新值!如:
example_dict = {'key_outer_01': {'key_inner_01': 'value_inner_01'},
'key_outer_02': {'key_inner_02': 'value_inner_02'}}
def get_key_value_of_nested_dict(nested_dict):
for key, value in nested_dict.items():
outer_key = None
inner_key = None
inner_value = None
if isinstance(value, dict):
outer_key = key
_, inner_key, inner_value = get_key_value_of_nested_dict(value)
else:
inner_key = key
inner_value = value
return outer_key, inner_key, inner_value
print get_key_value_of_nested_dict(example_dict)
# outputs : ('key_outer_01', 'key_inner_01', 'value_inner_01')
我相信我们需要更多信息,而所有边缘情况都需要知道这段代码是否合适。
我尝试了一些有趣的改进,告诉我它是否比原始代码更适合你的情况。
example_dict = {'key_outer_01': {'key_inner_01': 'value_inner_01', 'key_inner_02' : 'value_inner_02'},
'key_outer_02': {'key_inner_02': 'value_inner_02'},
'key_outer_03' : None}
def get_k_v_of_inner(nested_dict):
for k, v in nested_dict.items():
if isinstance(v, dict):
for k_nested, v_nested in v.items():
yield {k : {k_nested : v_nested}}
else:
# do_something, or simply ignore if not nested dict.
pass
gen = get_k_v_of_inner(example_dict)
for c, value in enumerate(gen):
print '#{} : {}'.format(c, value)
# outs : #0 : {'key_outer_01': {'key_inner_02': 'value_inner_02'}}
#1 : {'key_outer_01': {'key_inner_01': 'value_inner_01'}}
#2 : {'key_outer_02': {'key_inner_02': 'value_inner_02'}}
答案 1 :(得分:0)
如果你有一个嵌套的dict
,你需要一个嵌套的for
循环:
def print_items(nested_dict):
for key, dictionary in my_dict.items():
for inner_key, value in dictionary.items():
print(key, inner_key, value)
my_dict = {"a": {"b": "c", "d": "e"}, "f": {"g": "h"}}
print_items(my_dict)
打印:
a b c
a d e
f g h
答案 2 :(得分:0)
有一点可以肯定的是,你需要return
if isinstance(value, dict):
outer_key = key
get_key_value_of_nested_dict(value)
return get_key_value_of_nested_dict(value)
答案 3 :(得分:0)
example_dict = {'key_outer_01': {'key_inner_01': 'value_inner_01'}, 'key_outer_02': {'key_inner_02': 'value_inner_02'}}
def get_key_value_of_nested_dict(nested_dict, outer_key=None):
inner_key = None
inner_value = None
for key, value in nested_dict.items():
if isinstance(value, dict):
outer_key = key
return get_key_value_of_nested_dict(value, key)
else:
inner_key = key
inner_value = value
return outer_key, inner_key, inner_value
print get_key_value_of_nested_dict(example_dict)
<强>输出:强>
('key_outer_01', 'key_inner_01', 'value_inner_01')
答案 4 :(得分:0)
def get_key_value_of_nested_dict(nested_dict):
rv = []
for outer_key, value in nested_dict.items():
try:
# first we leap
inner_kvs = get_key_value_of_nested_dict(value)
for inner_keys, inner_value in inner_kvs:
rv.append(([outer_key]+inner_keys, inner_value))
except AttributeError:
# then ask for forgiveness
rv.append(([outer_key], value))
return rv
你得到一个元组列表。在每个元组中,第一个元素是键列表,第二个元素是最终值。
example_dict = {'key_outer_01': {'key_inner_01': 'value_inner_01'},
'key_outer_02': {'key_inner_02': 'value_inner_02'}}
get_key_value_of_nested_dict(example_dict)
# output
[(['key_outer_01', 'key_inner_01'], 'value_inner_01'),
(['key_outer_02', 'key_inner_02'], 'value_inner_02')]
要简单地获取没有内部键列表的元组列表,只需更改函数:
def get_key_value_of_nested_dict(nested_dict):
rv = []
for outer_key, value in nested_dict.items():
try:
inner_kvs = get_key_value_of_nested_dict(value)
for i_kvs in inner_kvs:
rv.append((outer_key,) + i_kvs)
except AttributeError:
rv.append((outer_key, value))
return rv
get_key_value_of_nested_dict(example_dict)
# output
[('key_outer_01', 'key_inner_01', 'value_inner_01'),
('key_outer_02', 'key_inner_02', 'value_inner_02')]