请问下列问题应该如何运用Python解决,求帮助

img

能用点个采纳吧

class Solution:
    def __init__(self, fileName):
        self.data = self.readFile(fileName)
        self.calculateScore()       # 经过计算数据

    def readFile(self, fileName):
        with open(fileName, 'r', encoding='utf-8') as f:      # 读取文件
            contents = f.readlines()     
        contents = [each.strip('\n') for each in contents]    # 去除换行符
        contents = [each.split(",") for each in contents]     # 按逗号分割(看你的题目中无空格)

        return contents

    def calculateScore(self):       
        for idx, content in enumerate(self.data):
            temp = content[:2]      # 将编号和名字放入其中
            # round为四舍五入
            score = round(int(content[2]) * 0.1 + int(content[3]) * 0.6 + int(content[4]) * 0.3)  # 加权成绩
            temp.append(score)
            temp.append(int(content[3]))      # 将高考成绩也放入其中
            self.data[idx] = temp

     # 人数输出格式
    def printPeople(self): 
        self.dataDict = {}       # 统计各个分数的人数
        for each in self.data:
            if each[2] in self.dataDict.keys():
                self.dataDict[each[2]] += 1
            else:
                self.dataDict[each[2]] = 1

        dataList = sorted(self.dataDict.items(), key=lambda x:x[0], reverse=True)  # 按分数进行排序
        print("分数          当前分数人数          累积人数")
        count = 0  # 累积人数
        for each in dataList:
            count += each[1]
            print("{}              {}                   {}".format(each[0], each[1], count))

    # 按录取格式输出
    def printRecruit(self):
        dataSorted = sorted(self.data, key=lambda x:x[2], reverse=True) # 按总分排序
        dataSortedgaokao = sorted(self.data, key=lambda x:x[3], reverse=True) # 按高考排序

        allStudent = len(dataSorted) # 总人数
        # 前百分之15, 没说怎么取整啊?!   如果总人数3个人, 3 * 0.15 = 0.45, 到底录取一个还是0个?
        allScore = int(allStudent * 0.15)     # 总分前15%的人数
        gaokaoScore = int(allStudent * 0.6)   # 高考前60%的人数
        gaokaoNumber = [dataSortedgaokao[i][0] for i in range(gaokaoScore)]

        for i in range(allScore):      # 在前百分之15中寻找高考前百分之60的人
            if dataSorted[i][0] in gaokaoNumber:
                print(dataSorted[i][0] + "," + dataSorted[i][1])

filename = 'data.txt'
x = Solution(filename)
x.printPeople()        # 按人数格式输出
x.printRecruit()     # 按录取格式输出
import chardet


def function(src_file):
    with open(src_file, 'rb') as f:
        data = f.read()
        result = chardet.detect(data)
        data = data.decode(result['encoding'])
        dataBase = []
        score_dict = {}
        for line in data.split('\n'):
            line = line.strip()
            tmpList = line.split(',')
            tmpList[2] = int(tmpList[2])
            tmpList[3] = int(tmpList[3])
            tmpList[4] = int(tmpList[4])
            tmpList.append(int(round(0.1 * tmpList[2] + 0.6 * tmpList[3] + 0.3 * tmpList[4])))
            dataBase.append(tmpList)
            score_dict[tmpList[-1]] = score_dict.get(tmpList[-1], 0) + 1
        dataBase = sorted(dataBase, key=lambda x: x[-1], reverse=True)  # 根据最后一列排序,逆序
        # 人数输出
        totalNum = 0
        print('人数:')
        print('{:10s} {:10s} {:10s}'.format('分数', '当前分数人数', '累计人数'))
        for k, v in sorted(score_dict.items(), key=lambda kv: kv[0], reverse=True):
            totalNum += v
            print('{:<9d} {:10d} {:10d}'.format(k, v, totalNum))

        tmpList = dataBase[:int(len(dataBase) * 0.15)]  # 前15%
        tmpList = sorted(tmpList, key=lambda x: x[3], reverse=True)  # 根据最后一列排序,逆序
        print('录取输出:')
        if len(tmpList) > 0:
            tmpList = tmpList[:int(len(tmpList) / 2)]
        if len(tmpList) > 0:
            for item in tmpList:
                print(item[0], item[1])


function('./data.txt')


img

img