简体   繁体   English

Python:查找列表列表的最大值和索引

[英]Python : find max value and index of a list of list of list

I have a list of list of list in python, which looks like this : 我有一个python列表列表,如下所示:

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

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; 关于第二个问题,请查看map功能; 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]

Why not use numpy ? 为什么不使用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]]])

So the solution I designed for the first problem first finds the max list from each row in the 3d list: 所以我为第一个问题设计的解决方案首先从3d列表中的每一行找到最大列表:

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函数

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

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]

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

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