So I have a list that contains several list which all have three strings first, then one float number, like:
resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]]
How do I make a function that returns the maximum value (which here would be 9.1931)? I tried
def MaxValue():
max_value = max(resultlist)
return max_value
but that just gives me a list.
EDIT: Also, any way I could get the index for where the value comes from? Like, from which sublist?
Here is an answer just in case you get a list of list where the number is not always on the 3rd position:
What is going on?
itertools.chain
flattens the list of lists, thefilter
then selects all the numeric values of which the maximal value is then determined using themax
function. Advantage here is that it also works for arbitrary lists of lists where the numeric value can be found anywhere in the list.For your example:
One more general example:
EDIT:
If you also want to get the index of the maximal value, you can do the following (using @Padraic Cunningham approach):
This approach assumes that there is exactly one numeric value per list!
One more example using a list where the numeric value is on an arbitrary position: