Python 深入阅读错综复杂的词典

Python 深入阅读错综复杂的词典,python,dictionary,Python,Dictionary,我有一个较大的字典,里面有较小的字典,我正在尝试获取里面的值,这些值不是字典(而是字符串、列表或整数),还有它们的“路径”。我尝试使用DFS(深度优先搜索)技术,但没有成功。这是我的大字典的一个示例: { 'visPlaneSection': { 'ValueMode': 'Single Section', 'OriginInput': '[0.4, 1.3877787807814457E-17, -8.4350929019372245E-16]

我有一个较大的字典,里面有较小的字典,我正在尝试获取里面的值,这些值不是字典(而是字符串、列表或整数),还有它们的“路径”。我尝试使用DFS(深度优先搜索)技术,但没有成功。这是我的大字典的一个示例:

{
    'visPlaneSection':
    {
        'ValueMode': 'Single Section',
        'OriginInput': '[0.4, 1.3877787807814457E-17, -8.4350929019372245E-16] m,m,m',
        'OrientationInput': '[1.0, 0.0, -3.3133218391157016E-16] m,m,m',
        'CoordinateSystem': 'Laboratory->Reference',
        'InputPartsInput': '[Region]',
        'ValueIndex': '-1',
        'visSingleValue':
        {
            'ValueQuantityInput': '0.0 m'
        }
    },
    'ChildrenCount': '2'
}
我需要的数据是:visPlaneSection.ValueMode='Single Section'

  • visPlaneSection.ValueMode='单节'

  • visPlaneSection.OriginInput=[0.4,1.3877778078714457E-17,-8.4350929019372245E-16]米,米,米

  • …等等
  • visPlaneSection.visSingleValue.ValueQuantityInput='0.0 m'
    我该怎么做?这可能是一个深入的树发现问题,但我不知道如何在我的问题上实现它
[编辑]:更具体地说,以下是我今天的内容:
def测试(词汇,路径=[]): 打印(路径) 如果类型(词汇)=词汇: 对于字典中的k.keys(): 如果路径==[]: 路径追加(k) 其他: 新路径=路径[-1]+“+k” append(新路径) 测试(词汇[k],路径) 其他: path.pop()

因此,在我向您展示的字典中,每个列表的最后一个元素都是我想要的路径,但它并不完美:
[]
['visPlaneSection']
['visPlaneSection','visPlaneSection.ValueMode']
['visPlaneSection','visPlaneSection.OriginInput']
['visPlaneSection','visPlaneSection.OrientationInput']
['visPlaneSection','visPlaneSection.CoordinationSystem']
['visPlaneSection','visPlaneSection.InputPartsInput']
['visPlaneSection','visPlaneSection.ValueIndex']
['visPlaneSection','visPlaneSection.visSingleValue']
['visPlaneSection','visPlaneSection.visSingleValue','visPlaneSection.visSingleValue.ValueQuantityInput']
['visPlaneSection','visPlaneSection.visSingleValue','visPlaneSection.visSingleValue.ChildrenCount']

我们这里有
visPlaneSection.visSingleValue.ChildrenCount
而不是
visPlaneSection.ChildrenCount
作为最后一个元素,这就是我的问题所在。

感谢您的耐心和解答

您需要如何处理这些数据?如果您只想打印它,您需要以下内容:

def _print_dict(s,d):
    for k,v in d.iteritems():
        dot = "." if s else ""
        p="{}{}{}".format(s,dot,k)
        if isinstance(v,dict):            
            _print_dict(p,v)
        else:
            print "{} = {}".format(p,str(v))
然后:

_print_dict('',d)

您没有提到用于实现这一点的python版本,python 3.4+使用
yield from
syntax等等看起来会更好。下面是a为python 2.7所做的:

def deconstruct(node, path=None):
    if not path:
        path = []

    for key, value in node.items():
        if isinstance(value, dict):
            for sub_value in deconstruct(value, path + [key]):
                yield sub_value
        else:
            yield (value, '.'.join(path + [key]))

if __name__ == '__main__':
    for value, path in deconstruct(input_dict):
        print path, '=', value

如果您担心执行速度-您可能希望用path变量中的字符串替换列表。

例如,single_section=d['visPlaneSection']['ValueMode']可能重复:嗯,我知道如何获得特定值,但我在阅读所有字典时遇到困难(使用递归算法)并获得每个数据的路径。您想要的最终形式是什么?对象?@RedX我需要值和路径作为两个不同的对象(str表示路径),这非常接近我要搜索的对象,但我需要路径(您的)来匹配每个数据。在这里,我与您的程序有:visPlaneSection.ValueMode.OriginInput.OriginationInput.CoordinationSystem.InputPartsInput.ValueIndex.visSingleValue.ValueQuantityInput.=0.0 m或者我需要visPlaneSection.visSingleValue.ValueQuantityInput='0.0 m'@m.Charlélie,你是对的,现在修复了代码但是
ChildrenCount
不属于
visPlaneSection
dict,为什么它会显示出来?是的,我刚才看到了,这是我的错误:)你的代码很棒,再次感谢你!