附加到列表

时间:2015-03-20 15:41:41

标签: python

我的calculate_grade函数遇到了一些问题。我试图将总数追加到每个列表的末尾(例如,查看我想要的输出)我试图使用for循环来完成此操作

for x in score_list:
    i.append(x)

但我没有得到我想要的输出。我如何设置它以便它不会保留上一个列表中的最后一个总数。

def get_data(file_object):
    while True:
        try:
            file_object=input("Enter the name of the input file: ")
            input_file=open(file_object, "r")
            break
        except FileNotFoundError:
            print("Error: file not found\n:")
    student_db=[]
    for line in input_file:
        fields=(line.split())
        name=int(fields[0])
        exam1=int(fields[1])
        exam2=int(fields[2])
        exam3=int(fields[3])
        in_class=int(fields[4])
        projects=int(fields[5])
        exercises=int(fields[6])
        record=[name,exam1,exam2,exam3,in_class,projects,exercises]
        student_db.append(record)
    student_db.sort()
    return student_db



def calculate_grade(all_students):
    #empty lists
    all_scores=[]
    score_list=[]
    #takes just scores from student_db and makes a list
    for scores in all_students:
        points=(scores[1:])
        all_scores.append(points)
    print("")
    print("")
    #makes seperate lists for each student 
    for i in all_scores:

        total_grade=0
        print(i)

        #totals up all of the scores for each student
        for j in i:
            total_grade+=j
            print(total_grade)
        #creates a list of the totals for each student
        score_list.append(total_grade)  
        #appends totals to each list of students
        for x in score_list:
            i.append(x)

    print("")
    print("")
    print("all_scores:",all_scores)
    print("")
    print("totals",score_list)



def main():
    all_students=get_data("data.tiny.txt")
    print(all_students)
    b=calculate_grade(all_students)
    print(b)

输入文件" data.tiny.txt"

 001      115   135   150    50   437    15
  002     120    125   172    50   378    15
   003    120     123   125    50   354    14
    004   122      139   171    48   439    15
     005   95        88   100    46   194    14

输出:

Enter the name of the input file: data.tiny.txt
[[1, 115, 135, 150, 50, 437, 15], [2, 120, 125, 172, 50, 378, 15], [3, 120, 123, 125, 50, 354, 14], [4, 122, 139, 171, 48, 439, 15], [5, 95, 88, 100, 46, 194, 14]]


[115, 135, 150, 50, 437, 15]
115
250
400
450
887
902
[120, 125, 172, 50, 378, 15]
120
245
417
467
845
860
[120, 123, 125, 50, 354, 14]
120
243
368
418
772
786
[122, 139, 171, 48, 439, 15]
122
261
432
480
919
934
[95, 88, 100, 46, 194, 14]
95
183
283
329
523
537


all_scores: [[115, 135, 150, 50, 437, 15, 902], [120, 125, 172, 50, 378, 15, 902, 860], [120, 123, 125, 50, 354, 14, 902, 860, 786], [122, 139, 171, 48, 439, 15, 902, 860, 786, 934], [95, 88, 100, 46, 194, 14, 902, 860, 786, 934, 537]]

totals [902, 860, 786, 934, 537]

需要修复的内容:

all_scores: [[115, 135, 150, 50, 437, 15, 902], [120, 125, 172, 50, 378, 15, 902, 860], [120, 123, 125, 50, 354, 14, 902, 860, 786], [122, 139, 171, 48, 439, 15, 902, 860, 786, 934], [95, 88, 100, 46, 194, 14, 902, 860, 786, 934, 537]]

all_scores的所需输出:

[[115, 135, 150, 50, 437, 15, 902], [120, 125, 172, 50, 378, 15, 860], [120, 123, 125, 50, 354, 14, 786], [122, 139, 171, 48, 439, 15, 934], [95, 88, 100, 46, 194, 14, 537]]

1 个答案:

答案 0 :(得分:0)

您始终将整个score_list附加到列表中:

#creates a list of the totals for each student
score_list.append(total_grade) 
#appends totals to each list of student
for x in score_list
    i.append(x)

如果您不想这样做,请不要这样做。而只是添加您要添加​​的值:

i.append(total_grade)