【问题标题】:Unexpected result while iterating over a dictionary and adding the key-values to an empty dictionary迭代字典并将键值添加到空字典时出现意外结果
【发布时间】:2021-05-11 18:33:55
【问题描述】:

谁能帮我理解下面的代码,这是关于字典的。

为什么在我添加最后一行之前 for 循环运行良好:

elif student_scores[score] <= 70:
    student_grades = student_scores[score]="Failed

我什至尝试使用“else”条件,但结果是一样的:"Failed"

如果我删除最后一部分,那么结果如预期:

{'Harry': 'Exceeds Expectations', 'Ron': 'Acceptable', 'Hermione': 'Outstanding', 'Draco': 'Acceptable', 'Neville': 62}

为什么它只给我最后一个条件的键值而删除其他所有内容?

代码:


    student_scores = {
      "Harry": 81,
      "Ron": 78,
      "Hermione": 99, 
      "Draco": 74,
      "Neville": 62,
    }
    student_grades = {}
    for score in student_scores:
      student_grades = student_scores  
      if student_scores[score] >= 91:
        student_grades = student_scores[score]="Outstanding"
      elif student_scores[score] >= 81:
        student_grades = student_scores[score]="Exceeds Expectations"
      elif student_scores[score] >= 71:
        student_grades = student_scores[score]="Acceptable"
      elif student_scores[score] <= 70:
        student_grades = student_scores[score]="Failed"
    
    print(student_grades)

结果:失败

非常感谢您的帮助。

谢谢。

【问题讨论】:

  • 您是否打算创建一个新字典,将比较结果映射到原始字典中的值?
  • 你好乍得,是的

标签: python loops dictionary


【解决方案1】:

ifelif 语句之后的每一行的开头不需要student_grades = 。只需拥有student_scores[score]="Outstanding" 之类的就可以了。

迭代变量score 也具有误导性。如果您随时打印出来,您会看到这是学生的姓名,而不是他们的分数。

【讨论】:

  • 非常感谢您的及时回复,我现在看到了我的错误,您是对的,我应该使用“学生”而不是“分数”,因为它具有误导性。另外,只是为了让我理解(我只是从 python 开始),在我的初始代码中,为什么它用最后一行结果替换最后的所有内容?
  • 你的最后一行做了两件事。首先,它执行student_scores[name]="Failed" 将字典 student_scores 的值设置为键名的“失败”,并返回字典 student_scores。然后它执行student_grades =... ,它将整个字典 student_grades 设置为(现在更新的)字典 student_scores。
【解决方案2】:

这是因为您刚刚获得学生成绩的最后一个值。

for score in student_scores:
  student_grades = student_scores  
  if student_scores[score] >= 91:
    student_grades = student_scores[score]="Outstanding"
  elif student_scores[score] >= 81:
    student_grades = student_scores[score]="Exceeds Expectations"
  elif student_scores[score] >= 71:
    student_grades = student_scores[score]="Acceptable"
  elif student_scores[score] <= 70:
    student_grades = student_scores[score]="Failed"

print(student_grades)

您的 print(student_grades) 在 for 循环之外,因此只需获取存储在学生成绩中的最后一个值。我建议您进行这样的更改。

student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
student_grades = student_scores
for name, score in student_scores.items():
    # We dont need to assign student_grades each time
    if student_scores[name] >= 91:
        student_grades[name] = "Outstanding"
    elif student_scores[name] >= 81:
        student_grades[name] = "Exceeds Expectations"
    elif student_scores[name] >= 71:
        student_grades[name] = "Acceptable"
    elif student_scores[name] <= 70:
        student_grades[name] = "Failed"

for grades in student_grades.items():
    print(grades)

【讨论】:

    【解决方案3】:

    您实际上是用分数替换了 student_grades 字典中的分数。如果您将最后一条语句更改为“打印 student_grades”,您将看到这一点。要将项目添加到 student_scores 字典中,您必须使用“append”修饰符。

    将 if 语句之后的每个语句更改为: student_grades["姓名"].append["学生姓名"] student_grades["Score"].append["Excceeds Expectatios"] 这会将元素添加到每个学生的 student_grades 字典中

    【讨论】:

    • 您的最终结果似乎替换了所有其他结果,只是因为它在循环之外。可以将其视为将消息放入信封中。每次处理循环时,都会删除先前的消息,并将当前学生的结果放入信封中。只有在所有学生都被处理后,才会读取(打印)消息。如果 print 语句在循环内,则每次都会读取消息(等级)。
    • Python 是一种解释型语言而不是编译型语言,因此会产生稀疏结构。留白很重要。行首的位置很重要。与编译语言中典型的“下一个”语句不同,重要的是语句的位置。如果您缩进“打印”语句以与“elif”对齐,它将在循环“内部”
    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多