分类词典

2024-09-27 00:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集

{6317637: {'name': 'Le Petit Souffle', 'Votes': 314, 'Cuisines': 'French, Japanese, Desserts', 'AverageCostfortwo': 1100, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.8', 'Ratingcolor': 'lightgreen', 'Ratingtext': 'Excellent'}, 6304287: {'name': 'Izakaya Kikufuji', 'Votes': 591, 'Cuisines': 'Japanese', 'AverageCostfortwo': 1200, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.5', 'Ratingcolor': 'lightgreen', 'Ratingtext': 'Excellent'}, 6300002: {'name': 'Heat - Edsa Shangri-La', 'Votes': 270, 'Cuisines': 'Seafood, Asian, Filipino, Indian', 'AverageCostfortwo': 4000, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.4', 'Ratingcolor': 'green', 'Ratingtext': 'Very Good'}, 6318506: {'name': 'Ooma', 'Votes': 365, 'Cuisines': 'Japanese, Sushi', 'AverageCostfortwo': 1500, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'No', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.9', 'Ratingcolor': 'lightgreen', 'Ratingtext': 'Excellent'}, 6314302: {'name': 'Sambo Kojin', 'Votes': 229, 'Cuisines': 'Japanese, Korean', 'AverageCostfortwo': 1500, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.8', 'Ratingcolor': 'lightgreen', 
'Ratingtext': 'Excellent'}, 18189371: {'name': 'Din Tai Fung', 'Votes': 336, 'Cuisines': 'Chinese', 'AverageCostfortwo': 1000, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'No', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.4', 'Ratingcolor': 'green', 'Ratingtext': 'Very Good'}, 6300781: {'name': 'Buffet 101', 'Votes': 520, 'Cuisines': 'Asian, European', 'AverageCostfortwo': 2000, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4', 'Ratingcolor': 'green', 'Ratingtext': 'Very Good'}, 6301290: {'name': 'Vikings', 'Votes': 677, 'Cuisines': 'Seafood, Filipino, Asian, European', 'AverageCostfortwo': 2000, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 'Aggregaterating': '4.2', 'Ratingcolor': 'green', 'Ratingtext': 'Very Good'}, 6300010: {'name': 'Spiral - Sofitel Philippine Plaza Manila', 'Votes': 621, 'Cuisines': 'European, Asian, Indian', 'AverageCostfortwo': 6000, 'Currency': 'Botswana Pula(P)', 'HasTablebooking': 'Yes', 'HasOnlinedelivery': 'No', 
'Aggregaterating': '4.9', 'Ratingcolor': 'lightgreen', 'Ratingtext': 'Excellent'}}

我尝试使用以下代码进行排序:

sorted(frontend_content,key=lambda x:frontend_content[x]['name']),但我没有零钱

我该如何根据一个特定的字段排序,比如姓名或投票


Tags: nonamecurrencyyesvotescuisineslightgreenbotswana
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:36

在python中不能对dict排序,因为它是无序的,所以需要首先将它转换为其他数据类型,如list of listlist of tuple

x = {6317637: {'name' : .... } }
sorted_x = sorted(x.items(),key = lambda kv : kv[1]['Votes'])

通过调用x.items(),您将它作为(key,value)的元组进行迭代。因此,kv[1]将是这里的值

相关问题 更多 >

    热门问题