如何获取具有抽象元素的列表中最深的列表?

sem*_*dic 4 python numpy list

这里我有一些包含任何特定元素的列表(abc

a = [2, 4, [9, 10]]
b = [1, 3, 5, 9, [11, 13, 14, 15, [16, 17, 19, 24]]]
c = [2, 4, [5, 11, 13, [14, 17, 29, [31, 19]]]]

npA = np.array(a, dtype = object)
npB = np.array(b, dtype = object)
npC = np.array(c, dtype = object)
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取每个列表中最深的列表a,,bc

print(npA[-1]) # it shows [9,10]
print(npB[-1][-1]) # it shows [16, 17, 19, 24]
print(npC[-1][-1][-1]) # it shows [31, 19]
Run Code Online (Sandbox Code Playgroud)

如何得到这个问题的概括?或者 NumPy 是否有任何内置函数可以直接处理这个问题?

Tom*_*ean 5

您可以递归地解决这个问题,无需 numpy:

from typing import List

def deepest_list(l: List) -> List:
    last_item = l[-1]
    if isinstance(last_item, list):
        return deepest_list(last_item)
    return l
Run Code Online (Sandbox Code Playgroud)

输出:

deepest_list(c)
[31, 19]
Run Code Online (Sandbox Code Playgroud)