如何在深层嵌套python字典中搜索值? 我想知道下面从任何深度嵌套的dict(通常)中查找值的代码是否有任何缺陷。 注:

如何在深层嵌套python字典中搜索值? 我想知道下面从任何深度嵌套的dict(通常)中查找值的代码是否有任何缺陷。 注:,python,python-3.x,Python,Python 3.x,我正在尝试开发通用方法,从给定的字典中获取任何值。 如果该值存在,则方法将返回True,否则返回False 还是有更好的解决方案来寻找价值 主要功能 在主函数中调用method1 在主函数中调用method2 我用以下数据进行了测试。它很好用 这似乎是正确的。我在将JSONL展平以便摄取到数据库表中时编写了类似的代码,这需要跟踪包含每个值和类型的元素的路径。和大多数Python一样,这会很慢。@Tim非常感谢您的反馈。是的,我确实觉得这会很慢。这似乎是正确的。我在将JSONL展平以便摄取到数据库

我正在尝试开发通用方法,从给定的字典中获取任何值。 如果该值存在,则方法将返回True,否则返回False

还是有更好的解决方案来寻找价值 主要功能 在主函数中调用method1 在主函数中调用method2 我用以下数据进行了测试。它很好用


这似乎是正确的。我在将JSONL展平以便摄取到数据库表中时编写了类似的代码,这需要跟踪包含每个值和类型的元素的路径。和大多数Python一样,这会很慢。@Tim非常感谢您的反馈。是的,我确实觉得这会很慢。这似乎是正确的。我在将JSONL展平以便摄取到数据库表中时编写了类似的代码,这需要跟踪包含每个值和类型的元素的路径。和大多数Python一样,这会很慢。@Tim非常感谢您的反馈。是的,我确实觉得这会很慢。
def find_value(each_consumed_data, search_value):
    for key, value in each_consumed_data.items():
        if value == search_value:
            logger.debug("got the value {}".format(value))
            return True
        elif isinstance(value, dict):
            from_inner_dict = __find_value_from_dict(value, search_value)
            if not from_inner_dict:
                continue
            else:
                return from_inner_dict
        elif isinstance(value, list):
            from_inner_list = __find_value_from_list(value, search_value)
            if not from_inner_list:
                continue
            else:
                return from_inner_list
        elif value != search_value:
            continue
    return False
def __find_value_from_dict(nested_dict_data, search_value):
    for each_key, each_value in nested_dict_data.items():
        if each_value == search_value:
            logger.debug("got the value {}".format(each_value))
            return True
        elif isinstance(each_value, dict):
            return __find_value_from_dict(each_value, search_value)
        elif isinstance(each_value, list):
            from_list = __find_value_from_list(each_value, search_value)
            if not from_list:
                continue
            else:
                return from_list
        elif each_value != search_value:
            continue
    return False


def __find_value_from_list(nested_list_data, search_value):
    """
    :param nested_list_data:
    :param search_value:
    :return:
    """
    for each_value in nested_list_data:
        if each_value == search_value:
            logger.debug("got the value {}".format(each_value))
            return True
        elif isinstance(each_value, dict):
            from_dict = __find_value_from_dict(each_value, search_value)
            if not from_dict:
                continue
            else:
                return from_dict
        elif isinstance(each_value, list):
            return __find_value_from_list(each_value, search_value)
        elif each_value != search_value:
            continue
    return False

data = {'json_data': {'timestamp': '2020-11-14T11:24:14.085102Z',
                               'source': 'dfdfdfs',
                               'type': 'send-invitation-action',
                               'id': 'eca0d90a-7afe-40f1-a2f6-2204f2a2a7de',
                               'version': '1.00',
                               'payload': {'invitationCode': '385478',
                                           'invitationCodeExpiration': 1605439454.084286,
                                           'inviteeEmail': 'emailcfff',
                                           'inviteeMsisdn': None,
                                           'targetId': '259662e5-b58a-4998-97ae-0ee55cf5bc39',
                                           'targetName': ['Hello_O123','hco.v1','hello'],
                                           'invitorPersonId': '8d795bd4-e87b-4b9a-9e41-1a3f73edb45f',
                                           'invitorPersonName': 'cdfdfs'}},
                 'topic': 'hco.h',
                 'partition': 0,
                 'offset': 26}

if __name__ == '__main__':
    print(find_value(data, '385478'))