[[0], [[1, 2], [3, 4]], [[5, 6, 7], [8, 9, 10], [11, 12, 13]]] I want to get the maximum value from the list.

Asked 2 years ago, Updated 2 years ago, 38 views

Hello, just like the title, [[0], [[1, 2, [3, 4]], [5, 6, 7], [8, 9, 10], [11, 12, 13]]] I want to get the maximum value from the list I have no idea how to approach it.

In the list, 1*1, 2*2, 3*3... I want to get the maximum value in the list of n*n-shaped matrices, so I wonder how to code. It would be nice if the listed list had the same form, but I'm asking because I can't find the answer even if I googled.

python list

2022-09-20 15:56

1 Answers

Please keep that in mind

def deepflatten(iterable, depth=None, types=None, ignore=None):
    if depth is None:
        depth = float('inf')
    if depth == -1:
        yield iterable
    else:
        for x in iterable:
            if ignore is not None and isinstance(x, ignore):
                yield x
            if types is None:
                try:
                    iter(x)
                except TypeError:
                    yield x
                else:
                    yield from deepflatten(x, depth - 1, types, ignore)
            elif not isinstance(x, types):
                yield x
            else:
                yield from deepflatten(x, depth - 1, types, ignore)

L = [[0], [[1, 2], [3, 4]], [[5, 6, 7], [8, 9, 10], [11, 12, 13]]]
max(list(deepflatten(L)))
13


2022-09-20 15:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.