从字典中获取嵌套数组 [英] Get nested arrays out of a dictionary

查看:133
本文介绍了从字典中获取嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个模拟的结果,以提取所有的结果,就是numpy数组。当使用简单的材料进行仿真时,我们可能会获得一个数组值的字典:

  {'material1':array,'material2 ':array,...} 

在更复杂的材料模拟中,我们最终得到了嵌套的字典,如:$ {

  {'material1':{'shellmaterial':array,'corematerial':array}} 

嵌套深度是任意的,我想做的是创建一个情节,的可用数组将返回给用户,以其嵌套命名。因此,例如,上述结构将如下所示:

  {'material1.shellmaterial':array,'material1.corematerial' :array} 

然后我们将它们放在一个下拉菜单中。以便在情节中轻松查看。有没有人有一个很好的方法来迭代任意嵌套的字典,并返回只有数组类型值,使用新的键如上所示?



结果必须以这种方式存储为json兼容性,所以我不能真正回去重构以避免这种情况。

解决方案

这是我用于语言处理的一些决策树的一个功能,它不是你想要的,但它是同样的基本思想。

  def nodeMapForDict(d):
node_map = []
node_path = []
def nodeRecursiveMap(d,node_path):
为key,val在d.items()中:
如果type(val)不是dict:node_map.append(node_path + [key])
如果type(val)是dict:
nodeRecursiveMap(val,node_path + [key])
nodeRecursiveMap(d,node_path)
返回node_map
/ pre>

这里是适合您的用例的:

  def flattenDict(d):
node_map = {}
node_path = []
def nodeRecursiveMap(d,node_path):
为key,val在d.items() :
如果type(val)不是dict:node_map ['。'。join(node_path + [key])] = val
如果type(val)是dict:
nodeRecursiveMap ,node_path + [key])
nodeRecursiveMap(d,node_path)
返回node_map

示例:

  d = {'d':[1,2,3,4],'e':{'b':{'c' :1'},'a':{'b':'c'}} 
在[49]中:flattenDict(d)
输出[49]:{'d':[1,2 ,3,4],'ebc':1,'a.b':'c'}


I'm trying to parse the results of a simulation to extract all of the results that are numpy arrays. When simulating over a simple material, we might get a single dictionary with array values:

{'material1':array, 'material2':array, ...}

In more complex material simulations, we end up with nested dictionaries like:

{'material1': {'shellmaterial':array, 'corematerial':array}}

The depth of the nesting is arbitrary, and what I want to do is create a plot where all of the available arrays are returned to the user, named by their nesting. So for example,the above structure would end up like:

{'material1.shellmaterial' : array, 'material1.corematerial' : array}

We'd then put these in a dropdown menu. for easy viewing in a plot. Does anyone have a good way to iterate through an arbitrarily nested dictionary and return only the array type values with the new keys as shown above?

Results have to be stored this way for json compatibility, so I can't really go back and refactor to avoid this.

解决方案

Here is a function I used with some decision trees for language processing, it's not what you want, but it's the same basic idea.

def nodeMapForDict(d):
    node_map = []
    node_path = [] 
    def nodeRecursiveMap(d, node_path): 
        for key, val in d.items():
            if type(val) is not dict: node_map.append(node_path + [key]) 
            if type(val) is dict: 
                nodeRecursiveMap(val, node_path + [key])
    nodeRecursiveMap(d, node_path)
    return node_map

And here is one that should fit your use case:

def flattenDict(d):
    node_map = {}
    node_path = [] 
    def nodeRecursiveMap(d, node_path): 
        for key, val in d.items():
            if type(val) is not dict: node_map['.'.join(node_path + [key])] = val 
            if type(val) is dict: 
                nodeRecursiveMap(val, node_path + [key])
    nodeRecursiveMap(d, node_path)
    return node_map

example:

d= {'d': [1, 2, 3, 4], 'e': {'b': {'c': 1}}, 'a': {'b': 'c'}}
In [49]: flattenDict(d)
Out[49]: {'d': [1, 2, 3, 4], 'e.b.c': 1, 'a.b': 'c'}

这篇关于从字典中获取嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆