본문 바로가기
프로그램

[파이썬] 문제 : 2차원 리스트의 정렬 및 합계,평균 구하고 결과 파일에 쓰기

by 오디세이99 2022. 12. 14.
728x90
반응형

numList = [["20221234","홍길동",90]
          ,["20221235","송중기",80]
          ,["20221236","유아인",90]
          ,["20221237","유혜진",100]
          ,["20221238","김유정",95]]

numList.sort(key=lambda x:-x[2])     # 점수 기준 내림차순 정렬. -x는 내임차순. [2]는 점수 요소

tot = 0
avg = 0
f = open('final.txt', 'w')            # 파일 열기. 쓰기로 열기
f.write(' 학  번   이  름  점수\n')   # 파일에 쓰기
print(' 학  번   이  름  점수')
for i in range(len(numList)):
    # print(numList[i][0] + '  ' + numList[i][1] + '  ' + str(numList[i][2]))
    f.write(numList[i][0] + '  ' + numList[i][1] + '  ' + str(numList[i][2]) + '\n')   # 파일에 쓰기
    print(numList[i][0] + '  ' + numList[i][1] + '  ' + str(numList[i][2]))   # 출력
    tot += numList[i][2]              # 합계 계산
    
print('='*22)
print(f"전체 점수 총점={tot}")
f.write(f"전체 점수 총점={tot}\n")    # 총정 쓰기
print(f"전체 점수 평균={(tot/len(numList)):.2f}")
f.write(f"전체 점수 평균={(tot/len(numList)):.2f}\n")   # 평균쓰기
f.close()                             # 파일 닫기

728x90
반응형

댓글