【问题标题】:Python: For loop in a for loop? [duplicate]Python:for循环中的for循环? [复制]
【发布时间】:2014-04-26 08:19:00
【问题描述】:

我刚刚学习了编程和python的基础知识,作为“挑战”的一部分,我必须使用for循环来打印出每个学生的数据。我需要能够只显示学生的姓名和他们的分数,而不仅仅是像这样显示字典:

  1. 打印学生姓名
  2. 打印学生的作业
  3. 打印学生的测验
  4. 打印学生的测试

打印学生的作业打印 学生的测验打印学生的测试

但我目前无法通过变量(在本例中为“学生”)访问字典的值。我必须在 for 循环中使用 for 循环吗?

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

students = [lloyd, alice, tyler]

for i in students:
    print i[i] #<------ I have no idea what to do here

请通过好的代码一步一步指导我(以便我理解它),任何答案将不胜感激。

【问题讨论】:

  • 我尝试打印学生[i],但它返回错误:“列表索引必须是整数,而不是字典”
  • 只需使用print i。轻松愉快。
  • 我需要能够只显示学生的姓名和他们的分数,而不仅仅是显示字典
  • 你能告诉逻辑计算分数吗?
  • 您能否将您的解决方案发布为答案而不是评论?

标签: python variables for-loop dictionary


【解决方案1】:
for d in students:
    print d["name"],d["homework"],d["quizzes"],d["tests"]

【讨论】:

  • 谢谢!这很有效,我从这个答案中学到了一些东西。
【解决方案2】:

您应该将i[i] 替换为i["name"]。当您迭代 students 时,您将在每次迭代中获取字典,并且实例值存储在 i 中。

为了访问字典值,需要使用key来访问(dict是key,python中的值对);在您的情况下,您想使用 name 作为密钥进行访问。

【讨论】:

    【解决方案3】:

    在你的 for 循环中 i 已经是学生了:

    for i in students:
        print i
    
    {'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]}
    

    然后您可以像这样显示所需的信息:

    print i['name']
    

    您需要阅读以下内容:https://docs.python.org/2/tutorial/datastructures.html

    【讨论】:

      【解决方案4】:

      注释代码:

      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]
      }
      
      students = [lloyd, alice, tyler]
      
      for student in students:
          print student # The dictionnary, so...
      
          for j in student:
              print student[j] # Print each value
      

      【讨论】:

        【解决方案5】:

        好的,试试这个:

        for i in students:
            for j in i:
                print "%s -> %s", % (j, i[j])
        

        这就是你要找的吗?

        【讨论】:

          【解决方案6】:

          试试这个:

              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]
              }
          
              students = [lloyd, alice, tyler]
          
              for i in students:
                total=0;
                for s in i['quizzes']:
                  total=total+s;
                print "Name: "+i['name']+" Score:"+str(total);
          

          DEMO

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-29
            • 2020-10-18
            相关资源
            最近更新 更多