Returns min / max multidimensional in Python?

I have a list in the form

[ [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] ... ] etc. 

I want to return the minimum value of c and the maximum value of c + f. Is it possible?

+6
source share
5 answers

For minimum c :

 min(c for (a,b,c),(d,e,f) in your_list) 

For maximum c+f

 max(c+f for (a,b,c),(d,e,f) in your_list) 

Example:

 >>> your_list = [[[1,2,3],[4,5,6]], [[0,1,2],[3,4,5]], [[2,3,4],[5,6,7]]] >>> min(c for (a,b,c),(d,e,f) in lst) 2 >>> max(c+f for (a,b,c),(d,e,f) in lst) 11 
+15
source

List of Understanding for Salvation

 a=[[[1,2,3],[4,5,6]], [[2,3,4],[4,5,6]]] >>> min([x[0][2] for x in a]) 3 >>> max([x[0][2]+ x[1][2] for x in a]) 10 
+4
source

You need to match your list to one that contains only the items you care about.

Here is one possible way to do this:

 x = [[[5, 5, 3], [6, 9, 7]], [[6, 2, 4], [0, 7, 5]], [[2, 5, 6], [6, 6, 9]], [[7, 3, 5], [6, 3, 2]], [[3, 10, 1], [6, 8, 2]], [[1, 2, 2], [0, 9, 7]], [[9, 5, 2], [7, 9, 9]], [[4, 0, 0], [1, 10, 6]], [[1, 5, 6], [1, 7, 3]], [[6, 1, 4], [1, 2, 0]]] minc = min(l[0][2] for l in x) maxcf = max(l[0][2]+l[1][2] for l in x) 

The contents of the min and max calls are what are called the β€œgenerator” , and are responsible for creating a mapping from the source data to the filtered data.

+1
source

Of course it is possible. You have a list containing a list of two-element lists, which themselves become lists. Your basic algorithm

 for each of the pairs if c is less than minimum c so far make minimum c so far be c if (c+f) is greater than max c+f so far make max c+f so far be (c+f) 
+1
source

Suppose your list is stored in my_list:

 min_c = min(e[0][2] for e in my_list) max_c_plus_f = max(map(lambda e : e[0][2] + e[1][2], my_list)) 
+1
source

Source: https://habr.com/ru/post/903940/


All Articles