如何在嵌套列表中排序值?



我有一个像这样的列表:

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

您可以将列表扁平化,然后使用具有所需键的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名

如果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

您可以使用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'])

相关内容

  • 没有找到相关文章

最新更新