题解 | #成绩排序#学习大佬思路

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

while True:
    try:
        num = int(input())
        n = int(input())
        dic = {}
        # 更新成绩:名字字典(相同成绩的名字合在一起作为一个值)
        for i in range(num):
            person = input().split()  # 返回一个列表,列表里每个元素为str类型
            if int(person[1]) in dic:  # 成绩后面加名字,重复名字也没有关系
                # 将值原来的字符串(名字:为列表类型)连接新的字符串(重复人的名字)
                dic[int(person[1])].append(person[0])
            else:
                dic[int(person[1])] = [person[0]]  # 新增为 成绩:[名字]这样的键值对
        if n == 0:
            # 使用 sorted() 函数可以直接对字典排序,
            # 排序后生成的是‘只包含’字典‘键’的列表,
            # 默认按’键‘的升序排列
            for i in sorted(dic, reverse=True):  # 降序排序
                for j in dic[i]:
                    print(j + " " + str(i))
        else:
            for i in sorted(dic):  # 升序排序
                for j in dic[i]:
                    print(j + " " + str(i))
    except:
        break

全部评论

相关推荐

牛油果甜奶昔:别的先不说,牛客还能内推护士?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务