相关疑难解决方法(0)

循环遍历所有嵌套字典值?

for k, v in d.iteritems():
    if type(v) is dict:
        for t, c in v.iteritems():
            print "{0} : {1}".format(t, c)
Run Code Online (Sandbox Code Playgroud)

我正在尝试遍历字典并打印出值不是嵌套字典的所有键值对.如果值是字典,我想进入它并打印出其键值对...等.有帮助吗?

编辑

这个怎么样?它仍然只打印一件事.

def printDict(d):
    for k, v in d.iteritems():
        if type(v) is dict:
            printDict(v)
        else:
            print "{0} : {1}".format(k, v)
Run Code Online (Sandbox Code Playgroud)

完整测试案例

字典:

{u'xml': {u'config': {u'portstatus': {u'status': u'good'}, u'target': u'1'},
      u'port': u'11'}}
Run Code Online (Sandbox Code Playgroud)

结果:

xml : {u'config': {u'portstatus': {u'status': u'good'}, u'target': u'1'}, u'port': u'11'}
Run Code Online (Sandbox Code Playgroud)

python dictionary

93
推荐指数
7
解决办法
18万
查看次数

怎么做 - python字典遍历和搜索

我有嵌套字典:

{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
          u'key1': {'attrs': {'entity': 'r',
                              'hash': '34njasd3h43b4n3',
                              'id': '4130-1'},
                    u'key2': {'attrs': {'entity': 'c',
                                        'hash': '34njasd3h43bdsfsd4n3',
                                        'id': '4130-1-1'}}},
          u'key3': {'attrs': {'entity': 'r',
                              'hash': '34njasasasd3h43b4n3',
                              'id': '4130-2'},
                    u'key4': {'attrs': {'entity': 'c',
                                        'hash': '34njawersd3h43bdsfsd4n3',
                                        'id': '4130-2-1'}},
                    u'key5': {'attrs': {'entity': 'c',
                                        'hash': '34njawersd3h43bdsfsd4n3',
                                        'id': '4130-2-2'}}}},
 'someohterthing': 'someothervalue',
 'something': 'somevalue'}
Run Code Online (Sandbox Code Playgroud)

给予id - 一个ids喜欢41304130-2-2.
导航到正确字典的最简单方法是什么?

就像给定id4130-2-1那样它应该到达字典key=key5

非xml方法请.

编辑(1):筑巢之间14的水平,但我知道我的嵌套前解析.

编辑(2):修复了代码.

**编辑(3):**再次固定代码的字符串值 …

python parsing struct dictionary nested

10
推荐指数
4
解决办法
3万
查看次数

在任意深度的嵌套字典上行走/迭代(字典代表目录树)

我几乎可以肯定有一个简单的解决方案,但我现在花了几个小时阅读和重读相同的一组相关结果,这些结果并没有完全解决我的问题.

这个问题的背景(包括完成但可以跳过这个)

出现这种情况是因为我希望用户能够从目录(以及任何子目录)中选择一组文件,不幸的是,Tkinter在文件对话框中选择多个文件的默认功能在Windows 7上被破坏(http:/ /bugs.python.org/issue8010).

因此,我试图通过另一种方法(仍然使用Tkinter)来表示目录结构:构建目录结构的传真,由标记和缩进复选框(在树中组织)组成.因此像这样的目录:

\SomeRootDirectory
    \foo.txt
    \bar.txt
    \Stories
        \Horror
            \scary.txt
            \Trash
                \notscary.txt
        \Cyberpunk
    \Poems
        \doyoureadme.txt
Run Code Online (Sandbox Code Playgroud)

看起来像这样(其中#代表一个checkbutton):

SomeRootDirectory
    # foo.txt
    # bar.txt
    Stories
        Horror
            # scary.txt
            Trash
                # notscary.txt
        Cyberpunk
    Poems
        # doyoureadme.txt
Run Code Online (Sandbox Code Playgroud)

使用我在ActiveState中找到的某个配方(见下文),可以很容易地从目录结构构建原始字典,但是当我尝试迭代我留下的精美嵌套字典时,我遇到了问题.而且我认为我需要迭代它以便使用树的漂亮网格表示来填充Tkinter帧.然后我希望通过解释哪些复选框为true或false来加载用户选择的各种文本文件.除了在修复深度的情况下迭代字典外,一切看起来都相当容易.

用更抽象的术语来说

要制作这些嵌套的词典,我使用的是ActiveState配方 - http://code.activestate.com/recipes/577879/.它实现了os.walk来制作如下的字典:

a={
    'SomeRootDirectory': {
        'foo.txt': None,
        'bar.txt': None,
        'Stories': {
            'Horror': {
                'horror.txt' : None,
                'Trash' : {
                    'notscary.txt' : None,
                    },
                },
            'Cyberpunk' : None
            },
        'Poems' : {
            'doyoureadme.txt' : None
        }
    } …
Run Code Online (Sandbox Code Playgroud)

python iteration tree recursion dictionary

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

dictionary ×3

python ×3

iteration ×1

nested ×1

parsing ×1

recursion ×1

struct ×1

tree ×1