Python词典 - 调用定义

时间:2013-09-07 17:25:05

标签: python function dictionary key definition

因此,我的目标是获取三个词典,并以这种格式打印出来:

  

姓名:“姓名”

     

家庭作业:“平均名单”

     

测验:“平均列表”

     

测试:“列表的平均值”

我在列表中包含了3个词典,但我在挖掘方面遇到了麻烦。我想在这里一步一步走。

所以在查看此代码之后:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd,alice,tyler]

def compute_grades(ourstudents):
    for student in ourstudents:
        print "Name: " + student["name"]
        print "Homework: ", sum(student["homework"]) / len(student["homework"])
        print "Quizzes: ", sum(student["quizzes"]) / len(student["quizzes"])
        print "Tests: ", sum(student["tests"]) / len(student["tests"])

compute_grades(students)

有没有办法可以进行基本检查,这样就可以在任何字典上使用了?

例如......

  1. 键定义是字符串吗?如果是这样,请将其打印出来!
  2. 键定义是列表吗?如果是,请运行此平均功能并打印结果。
  3. 我基本上只想最小化硬编码,并提出更优雅的解决方案。任何帮助表示赞赏!

4 个答案:

答案 0 :(得分:3)

你可以这样做,但根本没有错误检查或密钥排序:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd,alice,tyler]

def average(data):
    return sum(data) / float(len(data))

def compute_grades(students):
    for student in students:
        print('-'*23)
        for key, value in student.items():
            if isinstance(value, str):
                formatted_value = value
            else:
                formatted_value = average(value)
            print("%s: %s" % (key.title(), formatted_value))

compute_grades(students)

答案 1 :(得分:0)

您可以将其包装在try catch块中

def compute_grades(ourstudents):
    for student in ourstudents:
        for field, value in student.items():
            try:
                average=sum(value)/float(len(value))
                print "{0}: {1}".format(field.title(), average)
            except TypeError:
                print "{0}: {1}".format(field.title(), value)

答案 2 :(得分:0)

这似乎完美无缺:

lloyd = {
    "name": "Lloyd",
    "performance": [16.0, 19.0, 25.0, 14.4, 24.5, 16.2],
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "extra_credit": "OVER 9000!!!!",
    "research": [16, 24, 56, 78, 23],
    "missed_days": [1, 1, 2, 1],
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd,alice,tyler]
def average(data):
    return sum(data) / float(len(data))
def compute_grades(ourstudents):
    for student in ourstudents:
        print "-" * 15
        print student["name"]
        for field, value in student.items():
            try:
                average=sum(value)/float(len(value))
                print "{0}: {1}".format(field.title(), average)
            except TypeError:
                pass
compute_grades(students)

基于@Kurtis输入和随机附加信息测试

答案 3 :(得分:-1)

这似乎完美无缺:

lloyd = {
    "name": "Lloyd",
    "performance": [16.0, 19.0, 25.0, 14.4, 24.5, 16.2],
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "extra_credit": "OVER 9000!!!!",
    "research": [16, 24, 56, 78, 23],
    "missed_days": [1, 1, 2, 1],
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd,alice,tyler]
def average(data):
    return sum(data) / float(len(data))
def compute_grades(students):
    for student in students:
        print "-" * 15
        print student["name"]
        for key in student.keys():
            if isinstance(student[key], str):
                key_value = student[key]
            else:
                key_value = average(student[key])
            if key != "name":
                print("%s: %s" % (key.title(), key_value))
compute_grades(students)

基于@septi输入并使用随机额外信息进行测试