子列表的递归函数返回None

时间:2018-08-21 10:36:22

标签: python recursion

我正在子列表上运行一个递归函数,以在找到列表中的元素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)

3 个答案:

答案 0 :(得分: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的索引。

答案 1 :(得分:0)

我编写了与您的代码非常相似的代码: 取而代之的是使用列表,而不是使用列表,我使用字典来递归地标记可以在其中找到值的位置,然后使用列表+元组进行同样的操作。

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])])]

答案 2 :(得分:0)

我相信您的逻辑结构不正确,在机会过去之后正在寻找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)
>