【问题标题】:How to sort value in a nested list? [closed]如何对嵌套列表中的值进行排序? [关闭]
【发布时间】:2021-10-02 18:14:52
【问题描述】:

我有一个如下所示的列表:

[[{'city': 'Denver',
   'name': 'Sushi Kashiba',
   'estimated_cost': 180,
   'user_rating': {'average_rating': 4.9, 'votes': 4175},
   'id': 31},
  {'city': 'Denver',
   'name': 'Peninsula Grand Hotel',
   'estimated_cost': 120,
   'user_rating': {'average_rating': 4.9, 'votes': 644},
   'id': 32},
  {'city': 'Denver',
   'name': 'Plum by Bent Chair',
   'estimated_cost': 180,
   'user_rating': {'average_rating': 4.9, 'votes': 1657},
   'id': 36},
  {'city': 'Denver',
   'name': "R' ADDA",
   'estimated_cost': 120,
   'user_rating': {'average_rating': 4.9, 'votes': 4255},
   'id': 39},
  {'city': 'Denver',
   'name': 'Lord of the Drinks',
   'estimated_cost': 150,
   'user_rating': {'average_rating': 4.8, 'votes': 2086},
   'id': 34}],
 [{'city': 'Denver',
   'name': 'Hitch',
   'estimated_cost': 120,
   'user_rating': {'average_rating': 4.8, 'votes': 3277},
   'id': 311},
  {'city': 'Denver',
   'name': 'Hi Lounge',
   'estimated_cost': 120,
   'user_rating': {'average_rating': 4.7, 'votes': 1687},
   'id': 315},
  {'city': 'Denver',
   'name': 'Vedge',
   'estimated_cost': 10,
   'user_rating': {'average_rating': 4.7, 'votes': 3837},
   'id': 320},
  {'city': 'Denver',
   'name': 'Social',
   'estimated_cost': 140,
   'user_rating': {'average_rating': 4.6, 'votes': 7838},
   'id': 313},
  {'city': 'Denver',
   'name': 'BKC DIVE',
   'estimated_cost': 10,
   'user_rating': {'average_rating': 4.6, 'votes': 5687},
   'id': 316}],
 [{'city': 'Denver',
   'name': 'Yauatcha',
   'estimated_cost': 280,
   'user_rating': {'average_rating': 4.8, 'votes': 5359},
   'id': 330},
  {'city': 'Denver',
   'name': "Joey's Pizza",
   'estimated_cost': 80,
   'user_rating': {'average_rating': 4.6, 'votes': 8289},
   'id': 321},
  {'city': 'Denver',
   'name': 'Fun Republic Social',
   'estimated_cost': 90,
   'user_rating': {'average_rating': 4.6, 'votes': 2914},
   'id': 323},
  {'city': 'Denver',
   'name': 'English Kitchen',
   'estimated_cost': 150,
   'user_rating': {'average_rating': 4.6, 'votes': 1887},
   'id': 324},
  {'city': 'Denver',
   'name': 'Pi Bar and Kitchen',
   'estimated_cost': 160,
   'user_rating': {'average_rating': 4.6, 'votes': 1927},
   'id': 325}],
 [{'city': 'Denver',
   'name': 'Palladium Social',
   'estimated_cost': 140,
   'user_rating': {'average_rating': 4.9, 'votes': 4892},
   'id': 332},
  {'city': 'Denver',
   'name': 'Bayroute',
   'estimated_cost': 300,
   'user_rating': {'average_rating': 4.8, 'votes': 1466},
   'id': 333},
  {'city': 'Denver',
   'name': 'HOP : House of Party',
   'estimated_cost': 160,
   'user_rating': {'average_rating': 4.7, 'votes': 813},
   'id': 331},
  {'city': 'Denver',
   'name': 'Jimis Burger',
   'estimated_cost': 70,
   'user_rating': {'average_rating': 4.6, 'votes': 3680},
   'id': 335},
  {'city': 'Denver',
   'name': 'Garage Inc. Public House',
   'estimated_cost': 150,
   'user_rating': {'average_rating': 4.5, 'votes': 684},
   'id': 334}]]

我想比较average_rating的值并返回最高的前5名(如果数字相同则按原始顺序显示)

预期输出:

Sushi Kashiba
Peninsula Grand Hotel
Plum by Bent Chair
R' ADDA
Palladium Social

【问题讨论】:

  • 为什么不能像排序其他东西那样排序?
  • 与您的想法相反,Stackoverflow 不是免费的编码服务,也不是要取代现有的教程或文档。您应该发送honest attempt at the solution,然后然后在必要时询问有关它的具体问题。
  • 出于@martineau 所述的原因投反对票。厌倦了零努力的代码请求。
  • 感谢您的建议,我确实尝试了多种方法,这里的关键(基于以下解决方案)是我需要展平列表。
  • 在这种情况下,你应该在你的问题中包含你的尝试,如果没有其他原因只是为了证明你已经做出了努力。

标签: python json list sorting dictionary


【解决方案1】:

您可以展平列表,然后将sorted 与所需的密钥一起使用。

解决方案:

#assuming your list is "my_list"
flattened = [i for lst in my_list for i in lst]
>>> [d["name"] for d in sorted(flattened, 
                               key=lambda x: x["user_rating"]["average_rating"], 
                               reverse=True)[:5]]
['Sushi Kashiba',
 'Peninsula Grand Hotel',
 'Plum by Bent Chair',
 "R' ADDA",
 'Palladium Social']

解释:

  1. 使用列表理解将列表扁平化为列表

    flattened = [i for lst in my_list for i in lst]
    
  2. 对“average_rating”上的字典列表进行排序:

    sorted_dicts = sorted(flattened, key=lambda x: x["user_rating"]["average_rating"], reverse=True)
    
  3. 只保留前 5 个

【讨论】:

    【解决方案2】:

    如果lst 是您的问题列表:

    for val in sorted(
        [v for l in lst for v in l],
        key=lambda k: k["user_rating"]["average_rating"],
        reverse=True,
    )[:5]:
        print(val["name"])
    

    打印:

    Sushi Kashiba
    Peninsula Grand Hotel
    Plum by Bent Chair
    R' ADDA
    Palladium Social
    

    【讨论】:

      【解决方案3】:

      你可以使用 itertools

      import itertools
      # flattens list and take top 5. Negative sign because we want high->low
      res = sorted(list(itertools.chain(*l)), key=lambda x: -x['user_rating']['average_rating'])[:5] 
      for r in res:
          print(r['name'])
      

      【讨论】:

        猜你喜欢
        • 2013-02-06
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        • 2019-06-04
        • 2021-03-08
        • 1970-01-01
        相关资源
        最近更新 更多