Get the maximum value from a list with lists?

So, I have a list that contains several lists that first have three lines, and then one floating point number, for example:

resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]] 

How to create a function that returns the maximum value (which will be 9.1931 here)? I tried

 def MaxValue(): max_value = max(resultlist) return max_value 

but it just gives me a list.

EDIT: Also, in some way, can I get an index for where the value came from? How, from which sublist?

+5
source share
6 answers

Scroll through your external list and select the last item of each sublist:

 def max_value(inputlist): return max([sublist[-1] for sublist in inputlist]) print max_value(resultlist) # 9.1931 

It is also better if you save all function-related variables in the scope (pass the list as an argument and do not confuse the namespace by reusing the variable names).

+4
source

Perhaps more functional than the pythonic approach:

 >>> max(map(lambda x: x[3], resultlist)) 9.1931 

It begins by matching each element of the result list with a number and then finds max.

Intermediate Array:

 >>> map(lambda x: x[3], resultlist) [8.3931000000000004, 6.3231000000000002, 9.1930999999999994] 
+8
source
 resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]] print(max(map(lambda x: x[-1],resultlist))) 

Output:

 9.1931 
+2
source

If you also want to use the index, you can use enumerate with operator.itemgetter with map :

 from operator import itemgetter def max_val(l, i): return max(enumerate(map(itemgetter(i), l)),key=itemgetter(1))) 

What will return the max tuple with index:

 In [7]: resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]] In [8]: max_val(resultlist, -1) Out[8]: (2, 9.1931) 

Or just a regular gen exp:

 from operator import itemgetter def max_val(l, i): return max(enumerate(sub[i] for sub in l), key=itemgetter(1)) 
+2
source

Here is the answer just in case you get a list of the list where the number is not always in third position:

 from itertools import chain max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist))) 

What's happening? itertools.chain aligns the list of lists, filter then selects all numerical values ​​whose maximum value is determined using the max function. The advantage here is that it also works for arbitrary list lists, where a numerical value can be found anywhere in the list.

In your example:

 resultlist = [['1', '1', 'a', 8.3931], ['1', '2', 'b', 6.3231], ['2', '1', 'c', 9.1931]] max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist))) #prints 9.1931 

Another common example:

 myList = [[23, 34, 'a'],['b'],['t', 100]] max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(myList))) #prints 100 

EDIT:

If you also want to get the index of the maximum value, you can do the following (using the @Padraic Cunningham approach):

 from itertools import chain import operator resultlist = [['1', '1', 'a', 8.3931], ['1', '2', 'b', 6.3231], ['2', '1', 'c', 9.1931]] l = filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist)) # l: [8.3931, 6.3231, 9.1931] max(enumerate(l), key = operator.itemgetter(1)) #(2, 9.1931) 

This approach assumes that the list has exactly one numeric value!

Another example using a list in which a numeric value is in an arbitrary position:

 from itertools import chain import operator myList = [[23, '34', 'a'],['b', 1000],['t', 'xyz', 100]] l=filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(myList)) max(enumerate(l), key = operator.itemgetter(1)) #prints (1, 1000) 
+1
source

Are you trying to just get the maximum number from the floats (the last index on your list)? If so, then here is the solution.

 last_indices = [x[3] for x in resultlist] return max(last_indices) 
0
source

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


All Articles