Python 3:展平嵌套字典和字典中的列表
我正在处理一个复杂的嵌套字典和列表数据结构。我需要将数据展平并将所有嵌套项都置于0级。有关更多说明,请参见以下示例:
{a:1,b:2,c:{c1:[{c11:1,c12:2,c13:3},{c21:1,c22:2,c23:3}],d1:[{d11:1,d12:2,d13:3},{d21:1,d22:2,d23:3}]},x:1,y:2}
我需要将其展平为:
{a:1,b:2,c_c1_c11:1, c_c1_c12:2,c_c1_c13:3,c_c1_c21:1,c_c1_c22:2,c_c1_c23:3, c_d1,d11:1...and so on}
我从这篇文章的第一个答案中获得了参考,但是只有在我嵌套了字典的情况下它才可以工作,如果列表嵌套在字典中并且更多的词典嵌套在这些列表中,则它不能工作。
我对代码做了一些修改以适合我的用例,但是此代码不起作用
def flattenDict(d):
node_map = {}
node_path = []
def nodeRecursiveMap(d, node_path):
for key, val in d.items():
if ((type(val) is not dict)&(type(val) is not list)):
node_map['_'.join(node_path + [key])] = val
if type(val) is list:
def nodeListRecursion(val,node_path):
for element in val:
if ((type(element) is not dict)&(type(element) is not list)) : node_map['_'.join(node_path + [key])] = element
if type(element) is list: nodeListRecursion(element,node_map)
if type(element) is dict: nodeRecursiveMap(element, node_path + [key])
nodeListRecursion(val,node_path)
if type(val) is dict: nodeRecursiveMap(val, node_path + [key])
nodeRecursiveMap(d, node_path)
return node_map
当我在此处粘贴代码时,缩进变得混乱。但我真的很感谢在这里的任何帮助。
-
我认为您太过复杂了。您从具有键和值的字典开始。它的值可以是要递归的字典或词典列表,也可以不是,在这种情况下,请不要理会。所以:
def flatten(d): out = {} for key, val in d.items(): if isinstance(val, dict): val = [val] if isinstance(val, list): for subdict in val: deeper = flatten(subdict).items() out.update({key + '_' + key2: val2 for key2, val2 in deeper}) else: out[key] = val return out
给我
In [34]: nested = {'a': 1, 'b': 2, 'c': {'c1': [{'c11': 1, 'c12': 2, 'c13': 3}, {'c21': 1, 'c22': 2, 'c23': 3}], 'd1': [{'d11': 1, 'd12': 2, 'd13': 3}, {'d21': 1, 'd22': 2, 'd23': 3}]}, 'x': 1, 'y': 2} In [35]: flatten(nested) Out[35]: {'a': 1, 'b': 2, 'c_c1_c11': 1, 'c_c1_c12': 2, 'c_c1_c13': 3, 'c_c1_c21': 1, 'c_c1_c22': 2, 'c_c1_c23': 3, 'c_d1_d11': 1, 'c_d1_d12': 2, 'c_d1_d13': 3, 'c_d1_d21': 1, 'c_d1_d22': 2, 'c_d1_d23': 3, 'x': 1, 'y': 2}