Python:For循环中的For循环?
我刚刚学习了编程和python的基础知识,作为“挑战”的一部分,我必须使用for循环打印每个学生的数据。我需要能够只显示学生的名字和他们的分数,而不只是像这样显示字典:Python:For循环中的For循环?,python,variables,for-loop,dictionary,Python,Variables,For Loop,Dictionary,我刚刚学习了编程和python的基础知识,作为“挑战”的一部分,我必须使用for循环打印每个学生的数据。我需要能够只显示学生的名字和他们的分数,而不只是像这样显示字典: 打印学生的名字 打印学生的家庭作业 打印学生的测验 打印学生的试卷 打印学生的家庭作业打印 学生的测验打印学生的测验 但我目前无法通过变量(在本例中是“学生”)访问字典的值。我必须在for循环中使用for循环吗 lloyd = { "name": "Lloyd", "homework": [90.0, 97.0,
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
lloyd={
“姓名”:“劳埃德”,
“家庭作业”:[90.0,97.0,75.0,92.0],
“测验”:[88.0,40.0,94.0],
“测试”:[75.0,90.0]
}
爱丽丝={
“姓名”:“爱丽丝”,
“家庭作业”:[100.0,92.0,98.0,100.0],
“测验”:[82.0,83.0,91.0],
“测试”:[89.0,97.0]
}
泰勒={
“姓名”:“泰勒”,
“家庭作业”:[0.0,87.0,75.0,22.0],
“测验”:[0.0,75.0,78.0],
“测试”:[100.0,100.0]
}
学生=[劳埃德、爱丽丝、泰勒]
对于我的学生:
打印i[i]#您应该将i[i]
替换为i[“name”]
。当您迭代students
时,每次迭代都会得到字典,实例值存储在i
中
为了访问字典值,您需要使用key进行访问(dict是key,python中的值对);在您的情况下,您希望使用name
作为键进行访问。您应该将i[i]
替换为i[“name”]
。当您迭代students
时,每次迭代都会得到字典,实例值存储在i
中
for d in students:
print d["name"],d["homework"],d["quizzes"],d["tests"]
为了访问字典值,您需要使用key进行访问(dict是key,python中的值对);在您的情况下,您希望使用name
作为键进行访问。您应该将i[i]
替换为i[“name”]
。当您迭代students
时,每次迭代都会得到字典,实例值存储在i
中
for d in students:
print d["name"],d["homework"],d["quizzes"],d["tests"]
为了访问字典值,您需要使用key进行访问(dict是key,python中的值对);在您的情况下,您希望使用name
作为键进行访问。您应该将i[i]
替换为i[“name”]
。当您迭代students
时,每次迭代都会得到字典,实例值存储在i
中
for d in students:
print d["name"],d["homework"],d["quizzes"],d["tests"]
为了访问字典值,您需要使用key进行访问(dict是key,python中的值对);在您的情况下,您希望使用名称
作为键进行访问。好的,请尝试以下操作:
for d in students:
print d["name"],d["homework"],d["quizzes"],d["tests"]
for i in students:
for j in i:
print "%s -> %s", % (j, i[j])
这就是你要找的吗?好的,试试这个:
for i in students:
for j in i:
print "%s -> %s", % (j, i[j])
这就是你要找的吗?好的,试试这个:
for i in students:
for j in i:
print "%s -> %s", % (j, i[j])
这就是你要找的吗?好的,试试这个:
for i in students:
for j in i:
print "%s -> %s", % (j, i[j])
这就是您要找的吗?注释代码:
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
注释代码:
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
注释代码:
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
注释代码:
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
在你的循环中,我已经是学生了:
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']
您需要阅读以下内容:在您的循环中,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']
您需要阅读以下内容:在您的循环中,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']
您需要阅读以下内容:在您的循环中,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']
您需要阅读以下内容:尝试以下内容:
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);
试试这个:
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);
试试这个:
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);
试试这个:
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);
我尝试打印学生[I],但它返回一个错误:“列表索引必须是整数,而不是dict”,只需使用print I
。简单。我需要能够只显示学生的名字和他们的分数,而不只是显示字典。你能告诉计算分数的逻辑吗?你能把你的解决方案作为答案而不是评论发布吗?我尝试过打印学生[I],但它返回一个错误:“列表索引必须是整数,而不是dict”只需使用打印i
。简单。我需要能够只显示学生的名字和他们的分数,而不只是显示字典。你能告诉计算分数的逻辑吗?你能把你的解决方案作为答案而不是评论发布吗?我尝试过打印学生[I],但它返回一个错误:“列表索引必须是整数,而不是dict”只需使用打印i。简单。我需要能够只显示学生的名字和他们的分数,而不只是显示字典。你能告诉计算分数的逻辑吗?你能把你的解决方案作为答案而不是评论发布吗?我尝试过打印学生[I],但它返回一个错误:“列表索引必须是整数,而不是dict”只需使用打印i。简单。我需要能够只显示学生的名字和他们的分数,而不仅仅是显示字典。你能告诉计算分数的逻辑吗?你能把你的答案作为答案而不是评论吗?谢谢!这很有效,我从这个答案中学到了一些东西。谢谢!这很有效,我从这个答案中学到了一些东西。谢谢!这很有效,我从这个答案中学到了一些东西。谢谢!这个方法奏效了,我从这个答案中学到了一些东西。