【问题标题】:How to add more than 2 dictionaries together in python如何在python中将两个以上的字典一起添加
【发布时间】:2015-11-30 11:31:15
【问题描述】:

我对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]

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

【问题讨论】:

  • 您的意思是更新 dict 值或在 dicts 中添加值?
  • 完全不清楚“添加”字典的含义。你希望结果是什么样的?你能举一个 Python 数据结构的输入和预期输出的例子吗?另外,使用循环而不是复制相同的代码十次。
  • 如果你有 100 个学生呢?您会为所有这些复制粘贴该代码吗? :) 我建议研究使用循环;您会发现生成最终标记也变得更加容易。
  • @DanielSanchez 是的,我要求将 dict 值添加到另一个 dict..
  • 而我不使用循环的原因是,我对它们还不好:3 每次我在某处使用循环时,我都会搞砸。

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


【解决方案1】:

OP,在 cmets 中:

我不使用循环的原因是,我对它们还不好: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。很抱歉,很长的行;为了清楚起见,我只是想在同一行上注释每一行。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    相关资源
    最近更新 更多