Python 在有序字典中搜索任何字符串值

Python 在有序字典中搜索任何字符串值,python,python-2.7,collections,ordereddictionary,Python,Python 2.7,Collections,Ordereddictionary,我有一个嵌套的有序字典,即有序字典,其中一些值是包含进一步有序字典的有序字典。 当我使用collections模块并使用以下调用时 yOrDict = yaml_ordered_dict.load('ApplyForm.yml') 然后我从yml文件ApplyForm.yml中获得一个有序的字典yOrDict 假设yOrDict只有一个键(ApplyForm)和一个值,而这个值又是一个有序字典 进一步调用将提供另一个有序字典: yOrDictLevel1 = yOrDict.get('Appl

我有一个嵌套的有序字典,即有序字典,其中一些值是包含进一步有序字典的有序字典。 当我使用collections模块并使用以下调用时

yOrDict = yaml_ordered_dict.load('ApplyForm.yml')
然后我从yml文件
ApplyForm.yml
中获得一个有序的字典
yOrDict

假设yOrDict只有一个键(
ApplyForm
)和一个值,而这个值又是一个有序字典

进一步调用将提供另一个有序字典:

yOrDictLevel1 = yOrDict.get('ApplyForm')
假设
1
有六个键,其值包含有序字典

假设在某个地方,一个有序字典的值命名为
Block

python中是否有一个调用/函数/方法,通过该调用/函数/方法,我可以检查块是否出现在顶级有序字典中-
yOrDict

i、 e.我想检查yOrDict中是否有字符串“Block”?

这应该可以解决问题

def find_value(needle, container):
    # Already found the object. Return.
    if needle == container:
        return True

    values = None
    if isinstance(container, dict):
        values = container.values()
    elif hasattr(container, '__iter__'):
        values = container.__iter__()

    if values is None:
        return False

    # Check deeper in the container, if needed.
    for val in values:
        if find_value(needle, val):
            return True

    # No match found.
    return False
用法:

In [3]: d = { 'test': ['a', 'b', 'c'], 'd1': { 'd2': 'Block', 'a': 'b'} }

In [4]: find_value('Block', d)
Out[4]: True
编辑:测试值是否包含
指针

def find_value(needle, container):
    # Already found the object. Return.
    if isinstance(container, basestring) and needle in container:
        return True

    values = None
    if isinstance(container, dict):
        values = container.values()
    elif hasattr(container, '__iter__'):
        values = container.__iter__()

    if values is None:
        return False

    # Check deeper in the container, if needed.
    for val in values:
        if find_value(needle, val):
            return True

    # No match found.
    return False
它并不是更高级元字典的一种显式方法,因为它执行get并搜索值,但它的简洁性和技巧

def find_value(needle, container):
    # Already found the object. Return.
    if needle == container:
        return True

    values = None
    if isinstance(container, dict):
        values = container.values()
    elif hasattr(container, '__iter__'):
        values = container.__iter__()

    if values is None:
        return False

    # Check deeper in the container, if needed.
    for val in values:
        if find_value(needle, val):
            return True

    # No match found.
    return False
编辑对您评论的回复

if (("Beijing" in val) for key,val in yOrDict.get('ApplyForm')iteritems()):
    print "Found Beijing"

result=find_value('Block',yOrDictLevel1)if(result==True)print('Block found')else print('Block not found')
显示语法错误尝试以下列方式缩进:1。将代码放回勾选'code`2。在每行代码后面加两个空格,以换行。未成功缩进我的yOrDict如下所示:
OrderedDict([('ApplyForm',OrderedDict([('$type','Block'),('TITLE',OrderedDict([('$io','output'),('$type','text'),('$value','Beijing 3DS Application Form')))))]。
我如何在这本有序词典中查找北京?'result=find(value('Beijing',yOrDict)print('result',result)`将输出作为('result',False)您的问题指的是值相等,而不是包含。我会马上更新我的答案以反映你的第二个案例。编辑:更新了我的答案。我的约克郡看起来是这样的:OrderedDict([('ApplyForm',OrderedDict([('$type','Block'),('TITLE',OrderedDict([('$io','output'),('$type','text'),('value','Beijing 3DS Application Form'))))]))我如何在这个有序字典中搜索北京?`如果在约克郡中是“Beijing”.获取('ApplyForm.values'):打印()“发现北京价值”其他:印刷品“在约迪克特中未发现北京价值”“`所提供的产品在北京没有价值yOrDict@tauseef_india这是正确的,因为北京不是代码所在的地方,我可以将代码更改为更高级别的代码dictionary@tauseef_india而且,该词典的格式看起来非常糟糕,
OrderedDict([('ApplyForm',OrderedDict([('$type','Block'),('TITLE',OrderedDict([('$io','output'),('$type','text'),('$value','Beijing 3DS申请表')))))]]