파이썬 문제 풀이 부탁드려요 ㅠㅠ

파이썬 문제 풀이 부탁드려요 ㅠㅠ

작성일 2022.04.17댓글 1건
    게시물 수정 , 삭제는 로그인 필요

profile_image 익명 작성일 -

내용이 좀 많습니다... ㅎㅎㅎ

import csv def get_median_v2(data): data = sorted(data) centerIndex = len(data)//2 return (data[centerIndex ] + data[-centerIndex - 1])/2 datas = [] with open('tips.csv', newline='') as csvfile: rs = csv.reader(csvfile) header = next(rs) for d in rs: a, b, c, d, e, f = d datas.append([float(a), float(b), c, d, e, int(f)]) print("1. Count the number of users who had dinner on Sunday") count = 0 for data in datas: if data[3] == "Sun": count += 1 print(">>>", count) print() print("2. find the mean, median, max, and min of the last 50 rows in the total_bill column") #min = 999999 tot = sum([d[0] for d in datas[-50:]]) min_v = min([d[0] for d in datas[-50:]]) max_v = max([d[0] for d in datas[-50:]]) avg = tot / 50 median = get_median_v2([d[0] for d in datas[-50:]]) print(f">>> avg: {avg:.2f} median:{median} max:{max_v} min:{min_v}") print() print("3. Check whether the csv file has any duplicate rows. If so, print the rows") for i in range(len(datas)): for j in range(i+1, len(datas)): s1 = datas[i] s2 = datas[j] if s1 == s2: print(f">>> Duplicate row {i} and {j} Data is: {s1}") print() print("4. Find the rank of user 100 when sorting the rows by") print("the top rate(tip/total_bill) in deescending order.") print("if the csv file ehas any duplicate rows, please remove the row(s) first.") i = 0 while i < len(datas): j = i + 1 while j < len(datas): s1 = datas[i] s2 = datas[j] if s1 == s2: del datas[j] j+=1 i+=1 s = sorted(datas, key=lambda x: (x[1] / x[0])) print(f">>> Rank 100 is {s[99]}")

1번부터 4번 문제에 대한 전체 코드 입니다.

실행 결과 입니다. 도움되셨길 바라며 도움되셨다면 답변확정 부탁드립니다.