我想从一份清单中获取价值?不确定如何访问它?但以下是我所做的&;我得到了一个错误
类型错误:字符串索引必须是整数,而不是strlloyd = {
"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)如何访问“家庭作业”键并在列表中求和其值?
2)如何访问dict alice及其所有键的值之和?家庭作业+测验+带sum()函数的测试
3)如何访问“llyod”键并获取其len()列表?说“作业”
这就是我尝试过的,我也犯了同样的错误: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'])
如果有人能澄清我对上述三个问题的怀疑。