【问题标题】:Recursive function on sublist returning None子列表上的递归函数返回无
【发布时间】:2018-08-22 15:23:24
【问题描述】:

我在 sublist 上运行递归函数来搜索列表中的元素 check_value 一旦找到它,它会验证 other_value 是否是相应列表的第一项并最终返回索引。但当前代码返回 None。任何人都可以支持,因为我对子列表上的递归函数不太了解。

 def check_with_list(dd, check_value, other_value=None):
    global new_index

    for index, h in enumerate(dd):
        if isinstance(h, list):
            result = check_with_list(h, check_value)

            if result is not None:
                if other_value:
                    new = (index,) + result
                    if len(new) == 2:

                        if not dd[new[0]][0] == other_value:
                            result = None
                        else:
                            return (index,) + result


        elif h == check_value:
            return (index,)
    # value not found
    return None


dd = [
    "gcc",
    "fcc",
    ["scc", "jhh", "rrr"],
    ["www", "rrr", "rrr"],
    "mmm",
    ["qwe", ["ree", "rer", "rrr"], "ere"]
]
dd = check_with_list(dd, "rrr", "ree")

print(dd)

【问题讨论】:

  • 你想要的答案是什么?
  • 2,2 如果 'scc' 作为 other_value 传递,而 3,1 当 'www' 是 other_value。
  • 这需要递归吗?可以有更多级别的子列表吗?
  • 是的,还有更多的列表。
  • 如果传递 'ree' 作为其他值索引应该是 5,1,2。

标签: python recursion


【解决方案1】:
def check_with_list(dd, check_value, other_value=None):
        global new_index
    for index, h in enumerate(dd):
        if isinstance(h, list):
            result = check_with_list(h, check_value)

            if result is not None:
                if other_value:
                    new = (index,) + result
                    if len(new) == 2:

                        if dd[new[0]][0] == other_value:
                            result = None
                        else:
                            return (index,) + result


        elif h == check_value:
            return (index,)
    # value not found
    return None


dd = [
    "gcc",
    "fcc",
    ["scc", "jhh", "rrr"],
    ["www", "rrr", "rrr"],
    "mmm",
    ["qwe", ["ree", "rrr", "rrr"], "ere"]
]
dd = check_with_list(dd, "rrr", "ree")

我已从下面的行中删除了 not

如果不是 dd[new[0]][0] == other_value:

其他一切似乎都很完美。该代码有效并返回 dd 中第一次出现 check_value 的索引。

【讨论】:

  • 如果传递'ree'作为其他值索引应该是5,1,2,这里它返回第一个索引ie,2,2 – –
【解决方案2】:

我编写的代码与您的代码非常相似: 代替 od 使用列表,我使用字典递归地标记您可以找到值的位置,然后我使用列表 + 元组进行相同的操作。

import pprint


def check_with_list(dd, check_value):
    my_indexes = {}
    for index, h in enumerate(dd):
        if isinstance(h, list):
            result = check_with_list(h, check_value)

            if result is not None:
                my_indexes[index] = result
        elif h == check_value:
            my_indexes[index] = True
    return my_indexes


def check_with_list_2(dd, check_value):
    my_indexes = []
    for index, h in enumerate(dd):
        if isinstance(h, list):
            result = check_with_list_2(h, check_value)

            if result is not None:
                my_indexes.append((index, result))
        elif h == check_value:
            my_indexes.append(index)
    return my_indexes


dd = [
    "aaa",
    "bbb",
    ["bbb", "ccc", "bbb"],
    ["bbb", ["ccc", "aaa", "bbb"], "aaa"]
]

rv = check_with_list(dd, "bbb")  # (1,2(0,2),3(0,1(2)))
pprint.pprint(rv)
rv = check_with_list_2(dd, "bbb")  # (1,2(0,2),3(0,1(2)))
pprint.pprint(rv)

返回值

{1: True, 2: {0: True, 2: True}, 3: {0: True, 1: {2: True}}}
[1, (2, [0, 2]), (3, [0, (1, [2])])]

【讨论】:

    【解决方案3】:

    我相信你的逻辑结构不正确,在机会过去后寻找other_value。这是另一种结构方式:

    def check_with_list(structure, check_value, other_value=None):
    
        for index, item in enumerate(structure):
            path = (index,)
    
            if isinstance(item, list):
    
                sub_path = check_with_list(item, check_value, other_value)
    
                if sub_path is not None:
    
                    path += sub_path
    
                    if other_value and check_value in item:
    
                        if item[0] == other_value:
                            return path
                    else:
                        return path
    
            elif item == check_value:
                return path
    
        return None  # value not found
    
    dd = [
        "gcc",
        "fcc",
        ["scc", "jhh", "rrr"],
        ["www", "rrr", "rrr"],
        "mmm",
        ["qwe", ["ree", "rer", "rrr"], "ere"]
    ]
    
    print(check_with_list(dd, "rrr", "ree"))
    

    输出

    > python3 test.py
    (5, 1, 2)
    >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2021-08-03
      • 2021-04-16
      • 2020-07-10
      • 1970-01-01
      相关资源
      最近更新 更多