繁体   English   中英

如何在 python 中的嵌套列表上正确使用递归?

[英]How to properly use recursion on nested lists in python?

我已经用 python 进行了 4 周的编程,现在我们正在谈论递归。

问题陈述

我需要写一个 function 一个参数是一个列表; function 必须检查列表及其子列表是否满足标准:

  • 列表中的所有元素都是整数;

或者

  • 列表中的所有元素都是列表,
  • AND所有子列表也满足条件。

例如:

[1, 2]                 True
[[1, 2, [1]]]          False
[[1], [2], [[3], [1]]] True

等等。

到目前为止我的尝试

我尝试先检查宽度,然后检查 go 进入深度。 Go 每级更深一层。 但这不会起作用,因为在某些时候

[[1], [2], [[3], [1]]]

1, 2, [3], [1] 

这是错误的。

我意识到我需要先 go 进入深度,如果我不能 go 更深并检查我需要的一切 go 回来。 类似于二叉树的东西。 还有我的问题。 我不知道什么时候我很深,完成了回到我的“剩菜”清单。

def intSuperListe(list_to_check):
    for idx, item in enumerate(list_to_check):
        if idx + 1 < len(list_to_check):
            if type(list_to_check[idx]) == type(list_to_check[idx + 1]):
                if type(list_to_check[idx]) == int:
                    if len(list_to_check) == 1 or len(list_to_check) == 2:
                        return True
                    else:
                        continue
                elif type(list_to_check[idx]) == list:
                    intSuperListe(list_to_check[idx])
                return True
            return False
        elif type(list_to_check[idx]) == int:
            return True
        elif type(list_to_check[idx]) == list:
            intSuperListe(list_to_check[idx])
            break
        else:
            return False

这是我的尝试。

非常感谢任何帮助。

可以编写反映要求的代码:

如果列表中的所有项目都是int ,则返回True

如果列表中的所有项目都是list并且这些子列表也符合要求,则返回True

否则返回False

def intSuperListe(list_to_check):
    areInts = all(isinstance(item, int) for item in list_to_check)
    if areInts:
        return True

    areLists = all(isinstance(item, list) for item in list_to_check)
    if areLists:
        return all(intSuperListe(item) for item in list_to_check)

    return False


for item in [[1, 2], [[1, 2, [1]]], [[1], [2], [[3], [1]]]]:
    print(intSuperListe(item), item)

Output:

True [1, 2]
False [[1, 2, [1]]]
True [[1], [2], [[3], [1]]]
def intSuperListe(list_to_check):
    if all(map(lambda x:isinstance(x, int), list_to_check)):
        return True
    elif all(map(lambda x:isinstance(x, list), list_to_check)):
        return all(map(intSuperListe, list_to_check))
    return False
>>> a = [1, 2]
>>> b = [[1, 2, [1]]]
>>> c = [[1], [2], [[3], [1]]]
>>> print(intSuperListe(a))
True
>>> print(intSuperListe(b))
False
>>> print(intSuperListe(c))
True
def intSuperListe(lst):

    if all([type(l) == int for l in lst]):
        # if all elements of lst are ints then this is True
        return True
    elif all([type(l) == list for l in lst]):
        # if all elements of lst are lists, recursively check
        # all elements by calling intSuperListe on each
        return all([intSuperListe(l) for l in lst])
    else:
        # If all elements are not list or ints, then return
        # False
        return False


a = [1, 2]
b = [[1, 2, [1]]]
c = [[1], [2], [[3], [1]]]

print(intSuperListe(a))
print(intSuperListe(b))
print(intSuperListe(c))

>>> True
>>> False
>>> True

暂无
暂无

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

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