2016-12-01 2 views
0

以下の関数は、grade type,grade type number,gradeという形式のcsvファイルを取ります。例最初の10はassignment,assignment number,gradeです。この関数は、最初の10文字をリストに入れます。特定のデータを返すcsv関数を変更していますか?

私は何をする関数を取得しようとしていることはリターンです:
highest grade,assignment number
lowest grade,assignment number

def assigment(file): 
    res=[] 
    f = open(file,'r+') 
    reader = csv.reader(f) 
    for i, line in enumerate(reader): 
     if i < 10:  
     res.append((line[0],int(line[1]),int(line[2]))) 
     print max(res) 
+2

入力ファイルの数行を入力すると便利ですあなたの希望する出力として – Navidad20

答えて

2

インデックス[2]は、各課題についてグレードであり、それが数値である場合は、あなたが使用することができますsortは次のように機能します。

import csv 


def assigment(file): 

    res = [] 

    with open(file) as f: 
     reader = csv.reader(f) 

     for line in reader[:10]: 
      res.append((line[0],int(line[1]),int(line[2]))) 

    res.sort(key= lambda x: x[2]) 

    max = res[-1] 
    min = res[0] 
関連する問題