如何在 python 中同时添加 2 个以上的词典

How to add more than 2 dictionaries together in python

我对python比较陌生,我是学生,我做了一个计算总分和百分比的代码,我想把9个词典加在一起,我试过+,combine_dict、merge_dict,从最近几天开始我一直在寻找答案,但找不到任何有用的东西。代码是:

     name1= input("enter the name of the first student: ")
     marks = {}       
     subjects = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects:
           marks[subject] = float(input("Enter " + name1 + "'s " + subject + " marks: "))
           tota = sum(marks.values())
           average = float(tota) / len(marks)         

     print ("\n")

     name2= input("enter the name of the second student: ")
     marks1 = {} 

     subjects1 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects1:
           marks1[subject] = float(input("Enter " + name2 + "'s " + subject + " marks: "))
           tota1 = sum(marks1.values())
           average1 = float(tota1) / len(marks1)         

     print ("\n")

     name3= input("enter the name of the third student: ")

     marks2 = {}          

     subjects2 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects2:
           marks2[subject] = float(input("Enter " + name3 + "'s " + subject + " marks: "))
           tota2 = sum(marks2.values())
           average2 = float(tota2) / len(marks2)         

     print ("\n")

     name4= input("enter the name of the fourth student: ")
     marks3 = {} 



     subjects3 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects3:
           marks3[subject] = float(input("Enter " + name4 + "'s " + subject + " marks: "))
           tota3 = sum(marks3.values())
           average3 = float(tota3) / len(marks3)         

     print ("\n")

     name5= input("enter the name of the fifth student: ")

     marks4 = {} 


     subjects4 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects4:
           marks4[subject] = float(input("Enter " + name5 + "'s " + subject + " marks: "))
           tota4 = sum(marks4.values())
           average4 = float(tota4) / len(marks4)         

     print ("\n")

     name6= input("enter the name of the sixth student: ")


     marks5 = {}         

     subjects5 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects5:
           marks5[subject] = float(input("Enter " + name6 + "'s " + subject + " marks: "))
           tota5 = sum(marks5.values())
           average5 = float(tota5) / len(marks5)         

     print ("\n")

     name7= input("enter the name of the seventh student: ")

     marks6 = {} 


     subjects6 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects6:
           marks6[subject] = float(input("Enter " + name7 + "'s " + subject + " marks: "))
           tota6 = sum(marks6.values())
           average6 = float(tota6) / len(marks6)         

     print ("\n")

     name8= input("enter the name of the eighth student: ")
     marks7 = {} 


     subjects7 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects7:
           marks7[subject] = float(input("Enter " + name8 + "'s " + subject + " marks: "))
           tota7 = sum(marks7.values())
           average7 = float(tota7) / len(marks7)         

     print ("\n")

     name9= input("enter the name of the nineth student: ")
     marks8 = {} 


     subjects8 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects8:
           marks8[subject] = float(input("Enter " + name9 + "'s " + subject + " marks: "))
           tota8 = sum(marks8.values())
           average8 = float(tota8) / len(marks8)         

     print ("\n")

     name10= input("enter the name of the tenth student: ")

     marks9 = {} 


     subjects9 = ["Accounts","History","Geography","Chemistry","Computer Science","Maths","Add maths", "English"]

     for subject in subjects9:
           marks9[subject] = float(input("Enter " + name10 + "'s " + subject + " marks: "))
           tota9 = sum(marks9.values())
           average9 = float(tota9) / len(marks9)

     print ("\n")

     marks10= merge_dicts(marks, marks1, marks2, marks3, marks4, marks5, marks6, marks7, marks8, marks9)

     print ("total marks for whole class are " + marks10 )

我想做的是在 marks10 中将所有其他词典加在一起,例如

marks1=[90, 80, 90, 80, 90, 80, 90, 80]
marks2=[90, 80, 90, 80, 90, 80, 90, 80]
marks3=[90, 80, 90, 80, 90, 80, 90, 80]
marks4=[90, 80, 90, 80, 90, 80, 90, 80]
marks5=[90, 80, 90, 80, 90, 80, 90, 80]
marks6=[90, 80, 90, 80, 90, 80, 90, 80]
marks7=[90, 80, 90, 80, 90, 80, 90, 80]
marks8=[90, 80, 90, 80, 90, 80, 90, 80]
marks9=[90, 80, 90, 80, 90, 80, 90, 80]

我希望 marks10 成为 marks10=[810,720,810,720,810,720,810,720] 希望现在清楚了..

OP,在评论中:

and the reason I am not using loops is because, I am not good with them yet :3 everytime I use loop somewhere, I screw up.

您已经在使用 for loops 迭代 subjects! :)

无论如何,如果不使用 loops 遍历学生,你能做的最好的事情就是计算总数,就像

total = {}
for subject in subjects:
    total[subject] = marks1[subject] + marks2[subject] + marks3[subject] + marks4[subject] + marks5[subject] + marks6[subject] + marks7[subject] + marks8[subject] + marks9[subject] + marks10[subject]

... 所以这是一个将问题分成 3 个方法的实现,每个方法都有大量评论。我希望这个帮助ps :)

subjects = ["Accounts", "History", "Geography", "Chemistry", "Computer Science", "Maths", "Add maths", "English"]


def get_marks(i):
    name = input("Enter the name of student %d: " % i)  # (the name is only used for the subject prompts below)
    if not name:  # If the user doesn't enter a name, don't query for subjects either.
        return  # (implicitly returns None)
    marks = {}
    for subject in subjects:  # Loop over the subjects -- this is OP's code :)
        marks[subject] = float(input("Enter " + name + "'s " + subject + " marks: "))
    return marks


def get_student_marks():
    student_marks = []  # Gather the students' marks (as dicts) into this list.
    for i in range(10):  # Query for 10 students at most.
        marks = get_marks(i + 1)  # Get an individual student's marks as a dict.
        if not marks:  # If no name was entered (and None was thus returned)...
            break  # ... assume the user won't want to enter any more students and break out of the loop.
        student_marks.append(marks)  # Otherwise, save the marks...
    return student_marks  # and when the loop finishes, one way or another, return the list of dicts.


def print_total_marks(student_marks):
    total_marks = dict.fromkeys(subjects, 0)
    for marks in student_marks:  # Loop over each student...
        for subject, mark in marks.items():  # And then each pair of subject/mark...
            # (dict.fromkeys assures us that there's a key for every subject; no need to check at this point.)
            total_marks[subject] += mark  # ... and add that to the total.
    print ("Total marks for whole class are", total_marks)


student_marks = get_student_marks()
print_total_marks(student_marks)

(ps。抱歉,排长队;为了清楚起见,我只是想在同一行上注释每一行。)