How to find the maximum of sublists in arbitrary nested list?

Question:

I have a list C11 containing many sublists. I want to find the maximum element of each sublist. I present the current and expected outputs.

C11 = [[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]

for i in range(0,len(C11)):
    C2 = max(C11[i])
print(C2)

The current output is

[[353.856161, 0.0, 0.0], [0.0, 294.983702, 126.991664]]

The expected output is:

[[[353.856161],[282.754301]], [[294.983702]]]
Asked By: AEinstein

||

Answers:

List Comprehension

Code:-

C11=[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]

res=[[[max(sublist)] for sublist in lis] for lis in C11]

print(res)

Output:

[[[353.856161], [282.754301]], [[294.983702]]]
Answered By: Yash Mehta

Use this function in case the nesting-depth of the list is variable.

C11=[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]


def find_max(ls: list) -> list:
    # does the list contain only numbers?
    if all((isinstance(x, float) for x in ls)):
        # if  yes return simple max
        return [max(ls)]
    else:
        # apply the function one level deeper
        return [find_max(x) for x in ls]

print(find_max(C11))
Answered By: Sandwichnick

In case the depth is completely arbitrary and you want to keep the same nesting structure in the output, here is a recursive function that keeps going in levels until reaching a "leaf" (list with values and not lists) and takes the maximums:

def get_max(l):
    res = []
    if isinstance(l[0], list):
        for sub in l:
            res.append(get_max(sub))
    else:
        res.append(max(l))
    return res

print(get_max([[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]))

Will give:

[[[353.856161], [282.754301]], [[294.983702]]]
Answered By: Tomerikoo

Here is a quick recursive generalized approach. Should work with any level of varying nesting.

c11 = [[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]

result = []

def traverse(arr, result):
    if len(arr) > 0:
        if type(arr[0]) is list:
            # check if item inside list is another list
            for i in arr:
                result = traverse(i, result)
        else:
            # if its a list of number, get the max and add it to result
            result.append(max(arr))
    return result

print(traverse(c11, result))
Answered By: Saharsh

Using recursive is the best option. Recursive works for any length of list. Used a initial validation to check if list element is empty.

Code:

check=[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]

def recursive_max(lst):
    if len(lst) == 0:
        return []
    if isinstance(lst[0], list):
        min_val = []
        for val in lst:
            min_val.append(recursive_max(val))
        return min_val
    else:
        return [max(lst)]

print(recursive_max(check))

Output:

[[[353.856161], [282.754301]], [[294.983702]]]
Answered By: The6thSense

C11 array is multi dimension array need put in nested loop

C11 = [[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]
for i in range(0, len(C11)):
    for j in range(0, len(C11[i])):
        C2 = max(C11[i][j])
        print(C2)

Output

enter image description here

Answered By: Shah Meer

I have assumed that the depth of the list is random.

I have used a recursive approach to replace a last-level list with a list of its maximum value.

def recurse(l):
    for index in range(len(l)):
        if type(l[index]) == list:
            l[index] = recurse(l[index])
        else:
            l = [max(l)]
            return l
    return l

Example:

l = [[5,3,8], [1,2,4], [[[2,4],[11,12]],[5,9]]]
recurse(l)
print(l)

Output:

[[8], [4], [[[4], [12]], [9]]]

Your Example:

C11=[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]
recurse(C11)
print(C11)

Output:

[[[353.856161], [282.754301]], [[294.983702]]]

Note: The function updates the list inplace so if you want to restore the list, use a copy of the old list.

Answered By: Ash

Code:

C11=[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]], [[0.0, 294.983702, 126.991664]]]
C2=[]
for i in range(0,len(C11)):
    C2.insert(i, [])
    for j in range(0,len(C11[i])):
        C2[i].insert(j, [])
        C2[i][j].insert(0, max(C11[i][j]))
print(C2)

Output:

[[[353.856161], [282.754301]], [[294.983702]]]
Answered By: Sankar Subburaj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.