Finding Max Value in the Second Column of a Nested List

Finding max value in the second column of a nested list?

max(alkaline_earth_values, key=lambda x: x[1])

The reason this works is because the key argument of the max function specifies a function that is called when max wants to know the value by which the maximum element will be searched. max will call that function for each element in the sequence. And lambda x: x[1] creates a small function which takes in a list and returns the first (counting starts from zero) element. So

k = lambda x: x[1]

is the same as saying

def k(l):
return l[1]

but shorter and nice to use in situations like this.

How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)?

If your nested lists always have only one string at the first index (as in your example), then you sort your list of lists by max value using max() on a slice of each nested list excluding the first item. Then, just slice the final output based on the number of "top" results you want. Following is an example of getting the "top" 3 lists with max values.

list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]

# sort nested lists descending based on max value contained
sorted_list = sorted(list_of_lists, key=lambda x: max(x[1:]), reverse=True)

# slice first 3 lists (to get the "top" 3 max values)
sliced_list = sorted_list[:3]

print(sliced_list)
# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]

You could turn it into a simple function to get the top "x" number of nested lists (the loop after the function is purely to print something similar to your example).

def max_lists(data, num):
results = sorted(data, key=lambda x: max(x[1:]), reverse=True)
return results[:num]

list_of_lists = [['a',1,19,5],['b',2,4,6],['c',22,5,9],['d',12,19,20]]

top_three = max_lists(list_of_lists, 3)

print(top_three)
for x in top_three:
print(f'max value: {max(x[1:])} list: {x}')

# OUTPUT
# [['c', 22, 5, 9], ['d', 12, 19, 20], ['a', 1, 19, 5]]
# max value: 22 list: ['c', 22, 5, 9]
# max value: 20 list: ['d', 12, 19, 20]
# max value: 19 list: ['a', 1, 19, 5]

find the min and max values of nested list by index of element in nested list

You can do the following using the built-in map, min, max functions and the zip(*...) transpositioning pattern:

min_x = list(map(min, zip(*x)))
max_x = list(map(max, zip(*x)))

Or as pointed out by Chris_Rands, this shortened form will also work:

min_x = list(map(min, *x))
max_x = list(map(max, *x))

Or use comprehensions:

min_x = [min(col) for col in zip(*x)]
max_x = [max(col) for col in zip(*x)]

If you desperately want to do it one line:

min_x, max_x = zip(*[(min(col), max(col)) for col in zip(*x)])

Find largest value in index 0 of a nested list

You can use a list comprehension and apply max() function over result.

max_number = max([item[0] for item in data])

Output

135763365

Another approach is to specify key in the max function.

max(data, key=operator.itemgetter(0))[0]

Get max value and index of max value from last item in nested lists

Based on my first answer, and other answers below :

  1. Compute the end's max and find it in the sublists

    # Find the max of the ends value
    max_end = max(map(lambda x: x[-1], scoring_matrix))
    # Find the sublist whe the last if the overall max
    indexes = [index for index, item in enumerate(scoring_matrix) if item[-1] == max_end]
    # Group result
    result = [max_end, indexes]

    print(result)
  2. Keep the ends together and work on them

    # Put all ends value in a list
    ends = list(zip(*scoring_matrix))[2]
    # Iterate over the ends to find the overall max
    indexes = [i for i, v in enumerate(ends) if v == max(ends)]
    # Group result
    result = [max(ends), indexes]

    print(result)

How to get the max value in a nested list?

Try this:

import datetime

my_list = [
['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,012'],
['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0),'0,153'],
['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,114'],
['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,109'],
['Morocco', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,252'],
['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,012'],
['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,113'],
['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,116'],
['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,250'],
['Spain', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,266'],
['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,112'],
['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,025'],
['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,224'],
['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,256'],
['Italy', 'Fish', datetime.datetime(2020, 11, 17, 0, 0), '0,245']]

max_prices_by_country = {}

for item in my_list:
price = max_prices_by_country.setdefault(item[0], 0)
max_prices_by_country[item[0]] = max(price, float(item[-1].replace(',', '.')))

max_price_per_list = [str(price).replace('.', ',') for price in max_prices_by_country.values()]
print(max_price_per_list)

Python find max in a list of lists

max function also takes a key argument which specifies a one-argument ordering function.

>>> data = [["one", 1], ["three", 3], ["two", 2]]
>>> max(data, key=lambda x: x[1])
['three', 3]

Get max value from a list with lists?

Loop through your outer list and select the last element of each sublist:

def max_value(inputlist):
return max([sublist[-1] for sublist in inputlist])

print max_value(resultlist)
# 9.1931

It's also best if you keep all function related variables in-scope (pass the list as an argument and don't confuse the namespace by reusing variable names).



Related Topics



Leave a reply



Submit