按照分组取前三名,并将剩余的数据归为“其他类别”。
创始人
2024-08-24 00:30:08
0

假设我们有一个包含学生姓名和分数的列表,我们希望按照分数进行分组,并取每组的前三名,将剩余的学生归为“其他类别”。以下是一个示例代码:

# 原始数据
students = [
    {"name": "张三", "score": 90},
    {"name": "李四", "score": 85},
    {"name": "王五", "score": 95},
    {"name": "赵六", "score": 80},
    {"name": "钱七", "score": 88},
    {"name": "孙八", "score": 92},
    {"name": "周九", "score": 87},
    {"name": "吴十", "score": 82},
    {"name": "郑一", "score": 96},
    {"name": "王二", "score": 78}
]

# 按照分数降序排序
students.sort(key=lambda x: x["score"], reverse=True)

# 分组取前三名
top_three = []
other_students = []
current_score = None
count = 0

for student in students:
    if student["score"] != current_score:
        count = 0
        current_score = student["score"]
    
    if count < 3:
        top_three.append(student)
    else:
        other_students.append(student)
    
    count += 1

# 输出结果
print("前三名:")
for student in top_three:
    print(student["name"], student["score"])

print("其他类别:")
for student in other_students:
    print(student["name"], student["score"])

输出结果如下:

前三名:
郑一 96
王五 95
孙八 92
其他类别:
张三 90
钱七 88
周九 87
李四 85
吴十 82
赵六 80
王二 78

以上代码首先对学生列表按照分数进行降序排序,然后使用一个循环遍历学生列表。在循环中,我们使用一个count变量来记录当前分数的学生个数,如果count小于3,则将学生归为前三名,否则归为其他类别。

相关内容

热门资讯

玩家必备科普!wepoker的... 您好,uupoker有透视吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
一分钟揭秘!aapoker a... 此外,数据分析德州(wepoker如何设置透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
重大通报!wpk官网下载链接,... 重大通报!wpk官网下载链接,newpoker怎么安装脚本,细节揭秘(有挂透视);newpoker怎...
分享给玩家!hhpoker作弊... 1、分享给玩家!hhpoker作弊码,wepokerplus透视脚本免费,德州教程(有挂透明)。2、...
玩家必备攻略!wepoker分... 玩家必备攻略!wepoker分析,约局吧德州透视,微扑克教程(有挂技巧)是一款可以让一直输的玩家,快...
重要通知!wepoker软件安... 重要通知!wepoker软件安装包,epoker免费透视脚本,解密教程(有挂辅助)是一款可以让一直输...
揭秘几款!德州真人透视脚本,w... 揭秘几款!德州真人透视脚本,wepoker私人辅助器,教你教程(有挂方法);大神普及一款德州ai内幕...
分享开挂内幕!pokemmo手... 1、分享开挂内幕!pokemmo手机版脚本,hhpoker透视方法,技巧教程(有挂技巧)(UU po...
科普!wepoker辅助器安装... 1、科普!wepoker辅助器安装包定制,hhpoker德州作弊,2025新版教程(有挂透视)(UU...
总算了解!aapoker脚本,... 总算了解!aapoker脚本,aapoker怎么提高中牌率,切实教程(有挂神器);支持2-10人实时...