简体   繁体   English

在列表和字典列表中搜索值

[英]Search value in a list of lists and dictionaries

I'm trying to write a code that checks if a specific value in a list that contains dictionaries and/or lists is present, and it returns True or False.我正在尝试编写一个代码来检查包含字典和/或列表的列表中的特定值是否存在,它返回 True 或 False。

Note: I cannot change the structure of the data.注意:我无法更改数据的结构。

I manage to have this function that does the job for the current data type that I have:我设法让这个 function 为我拥有的当前数据类型完成工作:

def search_value_list_dic(value, list_dic):

    if any(isinstance(el, list) for el in list_dic):
        number_lists = len(list_dic)
        list_or = []
        for i in range(number_lists):            
            if value in list_dic[i]:  
                list_or.append('true')
            else:
                  list_or.append('false')
    
        if 'true' in list_or: return True
        else: return False
    
    
    if value in list_dic:
         return True
    if value not in list_dic:
         return False

I'm aware that this is not the most efficient way of doing it.我知道这不是最有效的方法。 Does anyone have any ideas on how to improve?有人对如何改进有任何想法吗?

I also would like to make it more generic, basically to search for every item on the list (even if it has list on list on list and so on).我还想让它更通用,基本上是搜索列表中的每个项目(即使它在列表中的列表中有列表等等)。

Example of data structure:数据结构示例:


[{'id': 'BuMnLoadRequestCfgBufferRequestOnHysteresis', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERREQUESTONHYSTERESIS', 'en': 'Hysteresis to start buffer cylinder loading', 'de': 'Einschalthysterese Puffer'}, 'range': None, 'testing_conditions': [[{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, 'OR', {'ordName': 'BuMnLoadHydMnIsFlowControlActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.hyd.mnIsFlowControlActive.1 == true', 'condition_testing': 'BuMn.load.hyd.mnIsFlowControlActive.1 == 1'}], {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 3}, {'id': 'BuMnLoadRequestCfgBufferRequestOffHysteresis', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERREQUESTOFFHYSTERESIS', 'en': 'Hysteresis to stop buffer cylinder loading', 'de': 'Ausschalthysterese Puffer'}, 'range': None, 'testing_conditions': [[{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, 'OR', {'ordName': 'BuMnLoadHydMnIsFlowControlActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.hyd.mnIsFlowControlActive.1 == true', 'condition_testing': 'BuMn.load.hyd.mnIsFlowControlActive.1 == 1'}], {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 3}, {'id': 'BuMnLoadRequestCfgBufferAHSOffset', 'view_id': 122, 'label': {'key': 'BUMN_LOAD_REQUEST_CFGBUFFERAHSOFFSET', 'en': 'Buffer to heat source set temperature increase', 'de': 'Temperaturanhebung Puffer zu Mischer/Wärmeerzeuger'}, 'range': None, 'testing_conditions': [{'ordName': 'BuMnLoadRequestMnIsActive', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.mnIsActive.1 == true', 'condition_testing': 'BuMn.load.request.mnIsActive.1 == 1'}, {'ordName': 'BuMnLoadRequestInAHSHydraulic', 'ordValue': '1', 'type': 'single', 'condition': 'BuMn.load.request.inAHSHydraulic.4 == BUFFER', 'condition_testing': 'BuMn.load.request.inAHSHydraulic.4 == 1'}], 'type': 'customkeypadonly_text_linear', 'user_level': 2}]

And the value I want to search: OR以及我要搜索的值:或

Thanks谢谢

Here I implemented simple function contains(x, val) that returns True if x contains val somewhere inside, and False otherwise.在这里,我实现了简单的 function contains(x, val)如果x在内部某处包含val ,则返回True ,否则返回False This function recursively calls itself to search inside all sub-structures at any depth.这个 function 递归调用自己在任何深度的所有子结构中搜索。

When searching inside dictionary I search only inside values.在字典中搜索时,我只搜索内部值。 To search also inside keys replace piece of code contains(v, val) with contains(v, val) or contains(k, val) .要在键内搜索,请将代码contains(v, val)替换为contains(v, val) or contains(k, val)

Next code starts with this function, then your example x data structure follows (I re-formatted it to be pretty), and at the end of code there are some tests.下一个代码以此 function 开头,然后是您的示例x数据结构(我将其重新格式化为漂亮),在代码末尾有一些测试。

Try it online! 在线尝试!

def contains(x, val):
    if isinstance(x, list):
        for e in x:
            if contains(e, val):
                return True
    elif isinstance(x, dict):
        for k, v in x.items():
            if contains(v, val):
                return True
    elif x == val:
        return True
    return False

x = [
    {
        "id": "BuMnLoadRequestCfgBufferRequestOnHysteresis",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERREQUESTONHYSTERESIS",
            "en": "Hysteresis to start buffer cylinder loading",
            "de": "Einschalthysterese Puffer",
        },
        "range": None,
        "testing_conditions": [
            [
                {
                    "ordName": "BuMnLoadRequestMnIsActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.request.mnIsActive.1 == true",
                    "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
                },
                "OR",
                {
                    "ordName": "BuMnLoadHydMnIsFlowControlActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.hyd.mnIsFlowControlActive.1 == true",
                    "condition_testing": "BuMn.load.hyd.mnIsFlowControlActive.1 == 1",
                },
            ],
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 3,
    },
    {
        "id": "BuMnLoadRequestCfgBufferRequestOffHysteresis",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERREQUESTOFFHYSTERESIS",
            "en": "Hysteresis to stop buffer cylinder loading",
            "de": "Ausschalthysterese Puffer",
        },
        "range": None,
        "testing_conditions": [
            [
                {
                    "ordName": "BuMnLoadRequestMnIsActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.request.mnIsActive.1 == true",
                    "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
                },
                "OR",
                {
                    "ordName": "BuMnLoadHydMnIsFlowControlActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.hyd.mnIsFlowControlActive.1 == true",
                    "condition_testing": "BuMn.load.hyd.mnIsFlowControlActive.1 == 1",
                },
            ],
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 3,
    },
    {
        "id": "BuMnLoadRequestCfgBufferAHSOffset",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERAHSOFFSET",
            "en": "Buffer to heat source set temperature increase",
            "de": "Temperaturanhebung Puffer zu Mischer/Wärmeerzeuger",
        },
        "range": None,
        "testing_conditions": [
            {
                "ordName": "BuMnLoadRequestMnIsActive",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.mnIsActive.1 == true",
                "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
            },
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 2,
    },
]

assert contains(x, 'OR')
assert contains(x, 'BuMnLoadRequestMnIsActive')
assert not contains(x, 'AND')

You will have to search recursively.您将不得不递归搜索。 Something like this?像这样的东西?

from typing import Any


data = [
    {
        "id": "BuMnLoadRequestCfgBufferRequestOnHysteresis",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERREQUESTONHYSTERESIS",
            "en": "Hysteresis to start buffer cylinder loading",
            "de": "Einschalthysterese Puffer",
        },
        "range": None,
        "testing_conditions": [
            [
                {
                    "ordName": "BuMnLoadRequestMnIsActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.request.mnIsActive.1 == true",
                    "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
                },
                "OR",
                {
                    "ordName": "BuMnLoadHydMnIsFlowControlActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.hyd.mnIsFlowControlActive.1 == true",
                    "condition_testing": "BuMn.load.hyd.mnIsFlowControlActive.1 == 1",
                },
            ],
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 3,
    },
    {
        "id": "BuMnLoadRequestCfgBufferRequestOffHysteresis",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERREQUESTOFFHYSTERESIS",
            "en": "Hysteresis to stop buffer cylinder loading",
            "de": "Ausschalthysterese Puffer",
        },
        "range": None,
        "testing_conditions": [
            [
                {
                    "ordName": "BuMnLoadRequestMnIsActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.request.mnIsActive.1 == true",
                    "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
                },
                "OR",
                {
                    "ordName": "BuMnLoadHydMnIsFlowControlActive",
                    "ordValue": "1",
                    "type": "single",
                    "condition": "BuMn.load.hyd.mnIsFlowControlActive.1 == true",
                    "condition_testing": "BuMn.load.hyd.mnIsFlowControlActive.1 == 1",
                },
            ],
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 3,
    },
    {
        "id": "BuMnLoadRequestCfgBufferAHSOffset",
        "view_id": 122,
        "label": {
            "key": "BUMN_LOAD_REQUEST_CFGBUFFERAHSOFFSET",
            "en": "Buffer to heat source set temperature increase",
            "de": "Temperaturanhebung Puffer zu Mischer/Wärmeerzeuger",
        },
        "range": None,
        "testing_conditions": [
            {
                "ordName": "BuMnLoadRequestMnIsActive",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.mnIsActive.1 == true",
                "condition_testing": "BuMn.load.request.mnIsActive.1 == 1",
            },
            {
                "ordName": "BuMnLoadRequestInAHSHydraulic",
                "ordValue": "1",
                "type": "single",
                "condition": "BuMn.load.request.inAHSHydraulic.4 == BUFFER",
                "condition_testing": "BuMn.load.request.inAHSHydraulic.4 == 1",
            },
        ],
        "type": "customkeypadonly_text_linear",
        "user_level": 2,
    },
]

def search(obj: Any, string_to_find: str) -> bool:
    """ Search through an object recursively to find a specific string.

    Args:
        obj (Any): The object to analyze
        string_to_find (str): The string to find

    Returns:
        bool: Whether the string was found or not
    """

    # Treat dictionaries as lists of values
    if isinstance(obj, dict):
        obj = list(obj.values())

    # Recurse for every element of a list
    if isinstance(obj, (list, tuple,)):
        for element in obj:
            if element == string_to_find or search(element, string_to_find):
                return True

    # Not found by default
    return False


found = search(data, "BuMnLoadRequestInAHSHydraulic")
print(f"String 'BuMnLoadRequestInAHSHydraulic' found: {found}")

found = search(data, "OR")
print(f"String 'OR' found: {found}")

found = search(data, "MYRANDOMSTRING")
print(f"String 'MYRANDOMSTRING' found: {found}")

Output: Output:

String 'BuMnLoadRequestInAHSHydraulic' found: True
String 'OR' found: True
String 'MYRANDOMSTRING' found: False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM