[英]Dictionaries and loops in Python 3.x
We have a group grade log:我们有一个小组成绩日志:
log = {
'Alex': [3, 7, 11, 10, 8],
'Ben': [6, 12, 4, 9, 9],
'Сarla': [5, 10, 7, 5, 9]}
Output "Best student X, he has the maximum score Y, minimum score Z"输出“最好的学生 X,他的最高分 Y,最低分 Z”
Solution解决方案
I have a dictionary, the first thing I decided to do was transform it and replace the values with new ones by hooking the maximum and minimum scores for each student.我有一本字典,我决定做的第一件事是转换它并通过挂钩每个学生的最高和最低分数来用新的值替换值。 After that, in a loop I wanted to go through only the maximum scores of each student and find the maximum average and, using it, return the key in the dictionary that corresponds to the meaning.
之后,在循环中,我只想遍历每个学生的最高分数并找到最大平均值,并使用它返回字典中与含义相对应的键。 But an error occurs that I cannot figure out.
但是发生了我无法弄清楚的错误。 After passing through the loop, the data is converted to int and it is impossible to work with them.
通过循环后,数据被转换为int,无法使用它们。 What to do?
该怎么办?
Code代码
log = {
'Alex': [3, 7, 11, 10, 8],
'Ben': [6, 12, 4, 9, 9],
'Carla': [5, 10, 7, 5, 9]}
log.update({"Alex":[min(log["Alex"])] + [max(log["Alex"])],
"Ben":[min(log["Ben"])] + [max(log["Ben"])],
"Carla":[min(log["Carla"])] + [max(log["Carla"])]})
for i,j in log.values():
print(max(i,j))
Output输出
3 11
4 12
5 10
Here is the problem, int object isn't itarible.这就是问题所在, int 对象不是 itarible。 I want to find the largest pair, that is max(j) , I have to go through, but then how can I return a pair of values at once, and not just the max(j) .
我想找到最大的一对,即max(j) ,我必须通过,但是我怎样才能一次返回一对值,而不仅仅是max(j) 。
This is how one finds the best student:这是找到最好的学生的方式:
def by_total_grade(name):
return sum(grades[name])
best_student = max(grades, key=by_total_grade)
Full example:完整示例:
grades = {
"Alex": [3, 7, 11, 10, 8],
"Ben": [6, 12, 4, 9, 9],
"Сarla": [5, 10, 7, 5, 9],
}
best_student = max(grades, key=lambda k: sum(grades[k]))
best_grades = grades[best_student]
print(
f"Best student {best_student}, "
f"max score {max(best_grades)}, "
f"min score {min(best_grades)}."
)
Output:输出:
Best student Ben, max score 12, min score 4.
I must admit it is not 100% clear to me want you want to achieve - anyway, this is my try (using the minimum amount of Python notions and mantaining your code as much as possible):我必须承认,我并不是 100% 清楚你想要实现的目标——无论如何,这是我的尝试(使用最少的 Python 概念并尽可能多地维护你的代码):
log = {
'Alex': [3, 7, 11, 10, 8],
'Ben': [6, 12, 4, 9, 9],
'Carla': [5, 10, 7, 5, 9]}
for key, vs in log.items():
log.update({key: [[min(vs)] + [max(vs)]]})
#log.update({"Alex":[min(log["Alex"])] + [max(log["Alex"])],
# "Ben":[min(log["Ben"])] + [max(log["Ben"])],
# "Carla":[min(log["Carla"])] + [max(log["Carla"])]})
current_max = 0
current_min = 100
best_student = "Unknown"
for name, vs in log.items(): # this retrieves key and value for each entry in the dictionary
mx = vs[1] # why are you comparing the mix with the max of each student? Should this always be j[1]?
mn = vs[0]
if mx > current_max:
current_max, current_min, best_student = mx, mn, name
print("Best student is " + best_student + " his top vote is " + str(current_max) + ". His lowest vote is " + str(current_min))
A few comments:几点意见:
It is safer to use a loop to run the log update: if, by any chance, you forget to run the update for 1 student manually, you will incur into troubles.使用循环来运行日志更新更安全:如果你忘记手动运行 1 个学生的更新,你会遇到麻烦。 The loop "makes sure" this does not happen
循环“确保”这不会发生
Why, when you run the update of your log dictionary, you update with a list of lists, where the first list always contains the min and the second the max?为什么,当您运行日志字典的更新时,您会使用列表列表进行更新,其中第一个列表始终包含最小值,第二个列表始终包含最大值? Can't you update using just a simple list, where the first element is the min, the second the max?
你不能只使用一个简单的列表来更新,其中第一个元素是最小值,第二个元素是最大值?
log.update({key: [min(vs), max(vs)]})
I don't understand why, in your loop, you check what the max is between 2 numbers (I am referring to m = max(vs[0])
, or max(i, j)
in your case) where you already know the first is the min and the second is the max.我不明白为什么,在你的循环中,你检查 2 个数字之间的最大值(我指的是
m = max(vs[0])
或max(i, j)
在你的情况下)你已经知道第一个是最小值,第二个是最大值。 If you apply the change described in the previous point, this simply becomes:如果您应用上一点中描述的更改,这将变成:
m = vs[1]
NOTE : I have modified the code so that also the minimum vote for the best student is printed.注意:我修改了代码,以便打印出对最佳学生的最低投票数。
current_max
and current_min
are initialized with low and high values respectively, so that the logic in the loop works (if the 2 numbers are outside the range of possible vote, the first student processes will immediately update both current_max
and current_min
). current_max
和current_min
分别用低值和高值初始化,以便循环中的逻辑起作用(如果 2 个数字超出可能的投票范围,第一个学生进程将立即更新current_max
和current_min
)。 You could have actually initialized current_max=0
, given that I imagine the vote in this framework cannot be negative您实际上可以初始化
current_max=0
,因为我认为这个框架中的投票不能为负
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.