如何做到这一点 - python字典遍历和搜索 [英] How to do this - python dictionary traverse and search

查看:114
本文介绍了如何做到这一点 - python字典遍历和搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套字典:

  {'key0':{'attrs':{'entity':'p' ,'hash':'34nj3h43b4n3','id':'4130'},
u'key1':{'attrs':{'entity':'r',
'hash'
'id':'4130-1'},
u'key2':{'attrs':{'entity':'c',
'hash' $$$$$$$$$$$哈希':'34njasasasd3h43b4n3',
'id':'4130-2'},
u'key4':{'attrs':{'entity':'c',
'哈希':'34njawersd3h43bdsfsd4n3',
'id':'4130-2-1'}},
u'key5':{'attrs':{'entity':'c',
'hash':'34njawersd3h43bdsfsd4n3',
'id':'4130-2-2'}}}},
'someohterthing':'someothervalue',
'something':'somevalue'}

给定一个 id - 所有 4130 4130-2-2

whats最简单的方法是导航到正确的字典?



喜欢给定的 id 4130-2-1 然后应该使用 key = key5





编辑(1):嵌套位于 1 4 级别,但我知道嵌套之前我解析。



编辑(2) strong>:修正代码。



**编辑(3):**再次固定代码 ids 的字符串值。请原谅造成混乱。这是最后我希望:)

解决方案

你的结构是不愉快的不规则。这是一个带有访问者函数的版本,该函数遍历 attrs 子字典。

  def walkDict(aDict,visitor,path =()):
在aDict中的k:
如果k =='attrs':
visitor ,aDict [k])
elif type(aDict [k])!= dict:
pass
else:
walkDict(aDict [k],visitor,path +(k, )

def printMe(path,element):
打印路径,元素

def filterFor(path,element):
if element [ id'] =='4130-2-2':
打印路径,元素

你会用这样的。

  walkDict(myDict,filterFor)
/ pre>

这可以变成一个生成器,而不是一个访问者;它将 yield path,aDict [k] 而不是调用访问者函数。



你会使用它一个for循环。

  for path,attrDict in walkDictIter(aDict):
#process attrDict ...


I have nested dictionaries:

{'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'}

given an id - one of all the ids like 4130 to 4130-2-2.
whats the easiest way to navigate to the correct dictionary?

Like if the given id is 4130-2-1 then it should reach the dictionary with key=key5

non xml approaches please.

Edit(1): The nesting is between 1 to 4 levels, but I know the nesting before I parse.

Edit(2): Fixed the code.

**Edit(3):**Fixed code again for string values of ids. Please excuse for the confusion created. This is final I hope :)

解决方案

Your structure is unpleasantly irregular. Here's a version with a Visitor function that traverses the attrs sub-dictionaries.

def walkDict( aDict, visitor, path=() ):
    for  k in aDict:
        if k == 'attrs':
            visitor( path, aDict[k] )
        elif type(aDict[k]) != dict:
            pass
        else:
            walkDict( aDict[k], visitor, path+(k,) )

def printMe( path, element ):
    print path, element

def filterFor( path, element ):
    if element['id'] == '4130-2-2':
        print path, element

You'd use it like this.

walkDict( myDict, filterFor )

This can be turned into a generator instead of a Visitor; it would yield path, aDict[k] instead of invoking the visitor function.

You'd use it in a for loop.

for path, attrDict in walkDictIter( aDict ):
    # process attrDict...

这篇关于如何做到这一点 - python字典遍历和搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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