【发布时间】:2016-04-15 15:54:33
【问题描述】:
我正在尝试剥离嵌套字典(只有 1 级深,例如:some_dict = {'a':{}, b:{}} 所有所有非零值和无值。
但是我不确定谁来正确地重新组装字典,下面给了我一个关键错误。
def strip_nested_dict(self, some_dict):
new_dict = {}
for sub_dict_key, sub_dict in some_dict.items():
for key, value in sub_dict.items():
if value:
new_dict[sub_dict_key][key] = value
return new_dict
【问题讨论】:
-
请提供示例输入和所需输出
标签: python dictionary