我想从一份清单中获取价值?不确定如何访问它?但这是我做的我得到了一个错误
TypeError:字符串索引必须是整数,而不是str

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]
}

for i in alice:
    print i['quizzes']

我正在学习python,我还有几个问题,如果有人能帮助我学习的话,那就是gr8帮助。提前谢谢。
1)如何访问“家庭作业”键并在列表中求和其值?
?homework + quizzes + tests with sum() function
?say 'homework'
this is what I tried & I got the same error for this one as well:
def average(x):
    for a in x:
       return sum(a['homework']) / len(a['homework'])
       return sum(a['quizzes']) / len(a['quizzes'])
       return sum(a['tests']) / len(a['tests'])

最佳答案

问题1(如何访问“家庭作业”键并在列表中对其值求和?)

>>> sum(lloyd['homework'])
354.0

问题2家庭作业+测验+测验
def findsum(x):
    return sum([sum(x['homework']),sum(x['quizzes']),sum(x['tests'])])

>>> findsum(lloyd)
741.0

问题3)如何访问“llyod”键并获取其len()列表?say 'homework'
>>> len(lloyd['homework'])
4

10-06 03:54