我有一本字典
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
和一个取平均值的函数
def average(numbers):
return float(sum(numbers))/len(numbers)
我想要python函数get_average()来读取字典。之后,我希望函数取字典中列表的平均值,并将该列表的平均值乘以一个特定的数字(对于作业,评分权重为.10,测验为.30,测试为.60),并给出加权作业,测验和测试的总和。我正在使用if语句来检查从字典中的for循环中选择的列表是否是作业,测验或测试。
def get_average(alice):
hw=0
qz=0
ts=0
for assignment in student:
if assignment== ["homework"]:
hw=average(assignment)*.10
if assignment==["quizzes"]:
qz=average(assignment)*.30
if assignment==["tests"]:
ts=average(assignment)*.60
return hw+qz+ts
这只返回零。它应该是91.15
答案 0 :(得分:2)
其他答案绰绰有余。我只是想尝试一些不同的东西。
使用此方法,您可以轻松扩展标记条件的深度,而不会使用所有额外的if
语句扩展函数的大小。它也更容易编辑,IMO,重复性较低,而且很重要。
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
def average(numbers):
return float(sum(numbers))/len(numbers)
def get_average(student):
criteria = {'homework': 0.10, 'quizzes': 0.30, 'tests': 0.60}
return sum(y * average(student[x]) for (x,y) in criteria.iteritems())
alice['average'] = get_average(alice)
print alice
希望这有帮助。
答案 1 :(得分:1)
通过字典循环可以获得字典中的键。您需要使用平均函数的值。您还可以使用iteritems:
同时遍历键和值这应该有效:
for assignment, scores in student.iteritems():
if assignment=="homework":
hw=average(scores)*.10
if assignment=="quizzes":
qz=average(scores)*.30
if assignment=="tests":
ts=average(scores)*.60
return hw+qz+ts
你也可以循环浏览dict而不是得分有学生[赋值],但每次循环迭代需要两次查找,所以最好使用iteritems。
答案 2 :(得分:0)
您的代码中存在一些错误:
assignment
在'name'
函数中迭代学生的密钥,即'homework'
,get_average()
等。因此,请将其与字符串'homework'
进行比较,而不是与列表['homework']
进行比较。assignment
包含密钥,因此您需要使用该密钥的值来调用average
,即student[assignment]
student
函数中迭代get_average()
变量,因此它应该是get_average(student)
而不是get_average(alice)
。演示:
>>> def get_average(student):
... hw=0
... qz=0
... ts=0
... for assignment in student:
... # assignment iterates over keys of student i.e., 'name', 'homework' etc. So compare it with string 'homework' not with list ['homework']
... if assignment== "homework":
... # as I said assignment contains keys, so you need to call average with value of that key i.e., student[assignment]
... hw=average(student[assignment])*.10
... if assignment=="quizzes":
... qz=average(student[assignment])*.30
... if assignment=="tests":
... ts=average(student[assignment])*.60
... return hw+qz+ts
...
>>> def average(numbers):
... return float(sum(numbers))/len(numbers)
...
>>> alice = {
... "name": "Alice",
... "homework": [100.0, 92.0, 98.0, 100.0],
... "quizzes": [82.0, 83.0, 91.0],
... "tests": [89.0, 97.0]
... }
>>> get_average(alice)
91.14999999999999
答案 3 :(得分:0)
如果你正在使用python 3,请使用items()
,因为iteritems()
已从此版本中删除:
def get_average(alice):
tot = 0
for tests, grades in alice.items(): #iterates over keys and values
if tests == "homework":
tot += average(grades)*0.1
if tests == "quizzes":
tot += average(grades)*0.3
if tests == "tests":
tot += average(grades)*0.6
return tot
另一种使用dict理解的方法:
def get_average(alice):
tot = 0
gen = {test: average(grades) for test, grades in alice.items() if isinstance(grades, list)}
for test, grades in gen.items():
if test == "homework":
tot += grades*0.1
if test == "quizzes":
tot += grades*0.3
if test == "tests":
tot += grades*0.6
return tot