Python 成绩统计

题目来源:

PTA


问题描述:

输入一个正整数n(n>=1),然后输入n行数据,表示的是某班各位同学的各科成绩,请统计出各门课程的最高分

Python 成绩统计_第1张图片


解决代码:

代码如下:

代码一:

此代码虽然能完成需求,但代码过于冗长,比较复杂

if __name__ == '__main__':
    n = int(input())
    judge = 0
    dmax = {
     }
    for i in range(0,n):
        scores = str(input())
        scores = scores.strip('{}')
        scos = scores.split(',')
        for j in scos:
            susc = j.split(':')
            subj = susc[0].strip('\'\'')
            scor = int(susc[1])
            if judge == 0:
                dmax[subj] = scor
            else:
                ishave = dmax.get(subj,'无')
                if ishave == '无':
                    dmax[subj]=scor
                else:
                    if int(dmax.get(subj)) >= scor:
                        continue
                    else:
                        dmax[subj] = scor
        judge += 1
    for x in dmax:
        print(x,dmax.get(x),sep=':')

代码二:

此代码虽然较代码一而言简短很多,但存在bug,希望各位帮助斧正

if __name__ == '__main__':
    m = int(input())
    dicts = {
     }
    for i in range(m):
        s = input().strip("{").strip("}").replace("'", "").split(",")
        if not bool(dicts):
            dicts = s.copy()
        else:
            for j in range(len(s)):
                dicts[j] = max(dicts[j], s[j])

    for i in range(len(dicts)):
        print(dicts[i], end="\n")

Python 成绩统计_第2张图片


你可能感兴趣的:(编程实例,#,数据分析,python)