如何在python中一起添加2个以上的词典

时间:2015-11-30 11:31:16

标签: python arrays python-3.x dictionary addition

我是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 )

我想要做的是在标记10中所有其他字典应该加在一起,例如

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]

我想要标记10 marks10 = [810,720,810,720,810,720,810,720] 希望现在很清楚..

1 个答案:

答案 0 :(得分:1)

OP,在评论中:

  

我不使用循环的原因是因为,我对它们并不好:3每次我在某处使用循环时,我搞砸了。

您已经使用for循环来迭代subjects! :)

无论如何,没有使用循环来迭代学生,你可以做的最好的计算总数是非常糟糕的

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个方法的实现,每个方法都有很多评论。我希望这会有所帮助:)

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。抱歉长线;我只是想在同一行上注释每一行,以便清楚。)