3

I have a list of list of list in python, which looks like this :

my_list = [ [[1,2,3],[4,3,2]] , [[2,1,9],[8,1,2]] , [[5,4,3],[1,6,7]] , ...]

would like to retrieve the max value of the list and its three indices.

I have seen how to do it for a list of list :

max_value, max_index = max((x, (i, j))
                           for i, row in enumerate(my_list)
                           for j, x in enumerate(row))

but I don't understand how to adapt it for a 3rd list.


Another question : Is there an easy way to apply the following operation on all the elements of my list ?

my_list = my_list - my_list[0] * 2
1
  • 1
    For your second question, look at the map function; it's designed for just this purpose. Commented Oct 30, 2017 at 23:03

4 Answers 4

5

Just extend the concept?

max_value, max_index = max((x, (i, j, k))
                       for i, row in enumerate(my_list)
                       for j, col in enumerate(row))
                       for k, x in enumerate(col))

For your second question, look at the map function; it's designed for just this purpose.

map(lambda x: x - my_list[0] - 2, my_list)

Example:

>>> my_list = [5, 20, 22, 13, 8, 1000]
>>> map(lambda x: x - my_list[0] * 2, my_list)
[-5, 10, 12, 3, -2, 990]
Sign up to request clarification or add additional context in comments.

Comments

4

Why not use numpy?

import numpy as np

lst = [[[1,2,3],[4,3,2]] , [[2,1,9],[8,1,2]] , [[5,4,3],[1,6,7]]]

a = np.array(lst)                       # Create numpy array from list

>>> a
Out[]:
array([[[1, 2, 3],
        [4, 3, 2]],

       [[2, 1, 9],
        [8, 1, 2]],

       [[5, 4, 3],
        [1, 6, 7]]])

>>> a.tolist()                          # And convert back to list if needed
Out[]: [[[1, 2, 3], [4, 3, 2]], [[2, 1, 9], [8, 1, 2]], [[5, 4, 3], [1, 6, 7]]]

>>> a.tolist() == lst
Out[]: True

Get the indices of the maximum with:

>>> np.argwhere(a == a.max())           # Indices where a is maximum
Out[]: array([[1, 0, 2]], dtype=int64)

And apply your operation with:

a -= a[0] * 2                           # Apply operation inplace

>>> a
Out[]:
array([[[-1, -2, -3],
        [-4, -3, -2]],

       [[ 0, -3,  3],
        [ 0, -5, -2]],

       [[ 3,  0, -3],
        [-7,  0,  3]]])

Comments

2

So the solution I designed for the first problem first finds the max list from each row in the 3d list:

my_max_list =  map(max, my_list)

Then incorporates your original solution for find the max element in a list of lists

max_value, max_index = max((x, (i, j))
                       for i, row in enumerate(my_max_list)
                       for j, x in enumerate(row)) 

For the second problem you can just use the map function

map(lambda x: x - my_list[0] * 2, my_list)

Comments

0

You can try this:

my_list = [ [[1,2,3],[4,3,2]] , [[2,1,9],[8,1,2]] , [[5,4,3],[1,6,7]]]
maximum, the_index = [(a, i) for i, a in enumerate(my_list) if a == max(my_list)][0]
new_list = [[[c-b[0]*2 for c in b] for b in i] for i in my_list]

1 Comment

Your algorithm is now quadratic, because you keep calculating max(my_list) in a loop...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.