【问题标题】:Find all items with the top 5 unique values based upon 2nd element in tuple list根据元组列表中的第二个元素查找具有前 5 个唯一值的所有项目
【发布时间】:2019-04-09 05:22:46
【问题描述】:

我想根据元组的第二个元素在元组列表中查找具有前 5 个最大值的所有元组项。 比如我有一个元组列表

x1 = [(a, 5), (b, 5), (c, 4), (d, 3), (e, 8), (f, 9), (g, 2), (h, 1)]

我想得到以下列表:

x2 = [(a, 5), (b, 5), (c, 4), (d, 3), (e, 8), (f, 9)]

由于第二个元素的前 5 个唯一值是 9、8、5、4、3 和 a、b 的值都为 5,因此它们都应包含在列表中。

关于如何实现这一点的任何想法? 谢谢!

【问题讨论】:

    标签: python list sorting tuples


    【解决方案1】:

    查找前 5 秒的元素:

    i = set(list({x[1] for x in x1})[-5:])
    

    过滤列表:

    x2 = list(filter(lambda x: x[1] in i, x1))
    

    甚至更好:

    ss = {x[1] for x in x1}
    if len(ss) > 5:
        i = list(ss)[-5]
        x2 = list(filter(lambda x: x[1] >= i, x1))
    else:
        x2 = x1
    

    输出:

    [('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
    

    【讨论】:

      【解决方案2】:
      x1 = [('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3), ('g', 2), ('h', 1)]
      x1.sort(key=lambda x: x[1], reverse=True)
      max5set = set()
      i = 0
      for _, num in x1:
          max5set.add(num)
          i += 1
          if (len(max5set) == 6):
              break
      print(x1[:i-1])
      

      输出:

      [('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3)]
      

      如果您想按字母顺序获取此元组列表,请执行

      print(sorted(x1[:i-1], key=lambda x: x[0]))
      

      输出将是

      [('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
      

      【讨论】:

        【解决方案3】:

        使用sorteditertools.groupby

        import itertools
        
        func = lambda x:x[1]
        res = []
        n_max = 5
        group_by = itertools.groupby(sorted(x1, key=func, reverse=True), key=func)
        for _ in range(n_max):
            res.extend(list(next(group_by)[1]))
        

        输出:

        [('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3)]
        

        如果要对最终输出进行排序,请再次使用sorted

        sorted(res, key=lambda x:x[0])
        

        输出:

        [('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
        

        【讨论】:

          【解决方案4】:

          使用 numpy:

          def my_fun(x1, k):
              import numpy as np
          
              x2 = np.asarray(x1)                         # Convert to numpy array
              val = np.unique(np.sort(x2[:,1]))[-k:]      # Sort index 1 & find top 'k' unique values
              idx = np.isin(x2[:,1], val)                 # Indices of rows to retain
          
              x2 = x2[idx].tolist()
              x2 = list(map(tuple, x2))                   # Convert back to list of tuples
              return x2
          
          >>> x1 = [('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9), ('g', 2), ('h', 1)]
          
          >>> my_fun(x1, 5)
          [('a', '5'), ('b', '5'), ('c', '4'), ('d', '3'), ('e', '8'), ('f', '9')]
          
          >>> my_fun(x1, 3)
          [('a', '5'), ('b', '5'), ('e', '8'), ('f', '9')]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-25
            • 2022-12-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多