【问题标题】:FInd the max value of a list of list if the first and second elements are the same如果第一个和第二个元素相同,则查找列表列表的最大值
【发布时间】:2021-09-16 09:24:32
【问题描述】:

我有他的清单:

[['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']]   

如果第一个和第二个值相同,我需要通过循环找到最大值,否则我取唯一值。

例如,这里我想要结果:

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

我已经试过了:

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))

【问题讨论】:

  • 你的逻辑不清楚,能详细解释一下吗?
  • 对于列表的每个元素,如果第一个和第二个值相同,我需要找到最后一个值的所有值。
  • 为什么预期结果中没有 1488 ['104','264','1425']?
  • 抱歉,仍然不清楚,什么是“找到第三个值的所有值”以及它如何匹配提供的预期输出?
  • 我不使用数值 3 (1488) 而是使用每个列表的最后一个值来查找最大值。

标签: python list max


【解决方案1】:

你的问题有点不清楚,但如果你想要每个子列表的第四个值等于最大第四个值,你可以使用这个:

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

如果重复的答案与其他数据一起出现,您可以删除它们,或者删除第三个值等。

【讨论】:

    【解决方案2】:

    假设您想在最后一列中查找具有最大整数值的元素,按前两列分组,您可以执行以下操作:

    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]
    
    

    您会在 stdlib 文档中找到对 groupby 的很好解释。

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多