【问题标题】:From list of tuples, find tuple with max value for a certain index , where the rest of the tuple is the same [closed]从元组列表中,找到某个索引的最大值的元组,其中元组的其余部分相同[关闭]
【发布时间】:2016-02-24 01:34:01
【问题描述】:

从元组列表中,我想识别元组,它们具有 tuple[2] 的最大值(即第三个值),但其他值相同

即来自

searchlist = [(a,b,2),(a,b,3),(a,b,4),(a,b,5),(x,y,2),(x,y,4)]

我要找

result = [(a,b,5),(x,y,4)]

【问题讨论】:

  • 你的代码在哪里,它到底有什么问题?
  • 目前还没有代码,因为我真的不知道从哪里开始。仅使用 lambda 或 itemgetter 进行排序将不遵守对于具有相同 tuple[0],tuple[1] 组合的每个元组子列表的 tuple[2] 的最大值的要求
  • @Bakuriu ,是的,我现在看到了。谢谢!

标签: python sorting tuples filtering


【解决方案1】:

Python sorting how to 告诉功能在 python 中的排序。 根据需要按降序使用reverse=True。

In [62]: sorted(searchlist, key=lambda a: (a[2], a[1], a[0]), reverse=True)
Out[62]: 
[('a', 'b', 5),
 ('x', 'y', 4),
 ('a', 'b', 4),
 ('a', 'b', 3),
 ('x', 'y', 2),
 ('a', 'b', 2)]

【讨论】:

    【解决方案2】:
    from itertools import groupby
    
    searchlist = [('a','b',2),('a','b',3),('a','b',4),('a','b',5),('x','y',2),('x','y',4)]
    result = [max(v, key = lambda x: x[2]) for g, v in groupby(sorted(searchlist), key = lambda x: x[:2])]
    

    【讨论】:

    • 你是对的,我已经更正了解决方案
    【解决方案3】:

    您可能首先sort 您的值,然后您将能够使用groupby() 将具有相同的前两个元素的值分组。最后,max() 可以检索到每个的最大值。

    from itertools import groupby
    
    a, b, x, y = "a", "b", "x", "y"
    searchlist = [(a,b,2),(a,b,3),(a,b,4),(a,b,5),(x,y,2),(x,y,4)]
    
    searchlist.sort()
    grouped = groupby(searchlist, key=lambda v: (v[0], v[1]))
    maximums = [max(groups) for value, groups in grouped]
    
    print(maximums)
    # [("a", "b", 5), ("x", "y", 4)]
    

    【讨论】:

    • 别忘了来自itertools import groupby(downvote 不是我的)
    • @AndrésPérez-AlbelaH。谢谢!
    【解决方案4】:

    这是我过去用来完成类似任务的东西

    def layered_sort(iterable, indices_to_sort_by, include_duplicates=True):
        # indices_to_sort_by is a list of indices to sort by
        result = []
        for item in iterable:
            index = find_place(item, result, indices_to_sort_by, include_duplicates)
            if index is not None:
                result.insert(index, item)
        return result
    
    
    def find_place(item, result, indices, include_duplicates=True):
        # in case result is empty
        # and n never gets initialized
        t_len = len(result)
        top = t_len
        bottom = 0
        n = 0
        while top>bottom:
            n = (top+bottom)/2
            sorted_item = result[n]
            for index in indices:
                ob1 = item[index]
                ob2 = sorted_item[index]
                # if they are equal, sort
                # by next highest priority
                if ob1 != ob2:
                    if sorted([ob1, ob2]) == [ob1, ob2]:
                        # ob1 belongs in front of ob2
                        top = n
                    else:
                        n += 1
                        bottom = n
                    # move on to next place in result
                    break
            # identical match, insert behind
            else:
                if include_duplicates:
                    return n
                else:
                    return
        return n
    

    【讨论】:

      【解决方案5】:

      我们可以循环遍历元组的搜索列表并检查第三个元素的值,并将前两个元素作为字典键,如下所示:

      val = 0
      for a, b, value in searchlist:
         key = "%s-%s" %(a,b)
         if value > val and key in dict_val:
           elem = (a, b, value)
         dict_val[key] = elem
      
      print dict_val.values()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 2020-12-25
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多