Home > Software design >  FInd the max value of a list of list if the first and second elements are the same
FInd the max value of a list of list if the first and second elements are the same

Time:09-17

I have his list of list :

[['191', '279', '1488', '1425']
['191', '279', '1488', '855']
['191', '279', '1488', '1140']
['191', '279', '1488', '285']
['191', '279', '1488', '665']
['191', '279', '1488', '570']
['104', '264', '1488', '1140']
['191', '279', '1488', '760']
['104', '264', '1488', '760']
['104', '264', '1488', '665']
['104', '264', '1488', '1425']
['104', '264', '1488', '285']
['104', '264', '1488', '855']]   

I need to find the max value with a loop if the first and second values are the same and if not I take the unique value.

For exemple, here I want the result :

maxlist = [['191','279','1488','1425'],['104','264','1425']]

I have tried this :

maxlist = []
    for i in res:
        i = i.split(";")
        finallist.append(i)


    for i in finallist:
         for x in finallist:
             if x[0] == i[0] and x[1] == i[1]:
                 maxlist.append(int(x[3]))
                 try:
                     finallist.remove(i)
                 except:
                     pass

         maxresult = max(maxlist)
         valueA = x[0]
         valueB = x[1]
         valueC = x[2]

         print(valueA str(" ") str(valueB) str(" ") str(valueC) str(" ") str(maxresult))

CodePudding user response:

Your question is kinda unclear but if you are wanting every sublist whose fourth value is equal to the maximum fourth value, you can use this:

maxlist = list(filter(lambda x: int(x[3]) == max(x for x in map(lambda x: int(x[3]), inputlist)),inputlist))

You could then remove duplicate answers if they appear with other data, or remove the third value, etc.

CodePudding user response:

Assuming you want to find the elements with the maximum integer value in the last column, grouped by the first two columns you could do the following:

from itertools import groupby
from operator import itemgetter

sample_data = [
    ["191", "279", "1488", "1425"],
    ["191", "279", "1488", "855"],
    ["191", "279", "1488", "1140"],
    ["191", "279", "1488", "285"],
    ["191", "279", "1488", "665"],
    ["191", "279", "1488", "570"],
    ["104", "264", "1488", "1140"],
    ["191", "279", "1488", "760"],
    ["104", "264", "1488", "760"],
    ["104", "264", "1488", "665"],
    ["104", "264", "1488", "1425"],
    ["104", "264", "1488", "285"],
    ["104", "264", "1488", "855"],
]

# groupby needs data do be sorted by the same key.
sorted_data = sorted(sample_data, key=itemgetter(0, 1))

# group 4-tuples by the first two values. 
grouped = groupby(sorted_data, key=itemgetter(0, 1))


def get_last_element_value(element):
    # We need the integer value of the string to compare the elements,
    # so we cannot use itemgetter and instead use our own key function.
    return int(element[-1])


result = [max(elements, key=get_last_element_value) for _, elements in grouped]

You'll find a pretty good explanation of groupby in the stdlib documentation.

  • Related