[英]Iterating through dictionary values in python (.keys() in .values()?)
In a programming course I am now at dictionaries point in python.在编程课程中,我现在正在使用 python 的字典点。 So I have this task in which I need to add a "fail" command, which basically is printing out students if a grade is less than 4. I have googled it and searched for similar problems in here, but just couldn't find a similar example.所以我有这个任务,我需要添加一个“失败”命令,如果成绩低于 4,它基本上是打印出学生。我用谷歌搜索并在这里搜索了类似的问题,但找不到类似的例子。 Hope you can help me.希望您能够帮助我。 Also, I have added the code and in "def fail():" you can see what was my idea.另外,我已经添加了代码,在“def fail():”中你可以看到我的想法。 But there is an error code with this - ValueError: too many values to unpack (expected 2).但是这里有一个错误代码 - ValueError: too many values to unpack (expected 2)。 PS, I'm new to python. PS,我是python的新手。
students = {('Ozols', 'Jānis'): {'Math': '10', 'ProgVal': '5', 'Sports': '5'},
('Krumiņa', 'Ilze'): {'Math': '7', 'ProgVal': '3', 'Sports': '6'},
('Liepa', 'Peteris'): {'Math': '3', 'ProgVal': '7', 'Sports': '7'},
('Lapsa', 'Maris'): {'Math': '10', 'ProgVal': '10', 'Sports': '3'}}
courses = ['Math', 'ProgVal', 'Sports']
def fail():
for lName, fName in students.keys():
for course, grade in students.values():
if grade < 4:
print(fName, lName)
while True:
print()
command = input("command:> ")
command = command.lower()
if command == 'fail':
fail()
elif command == 'done':
break
print("DONE")
Try the below试试下面的
students = {
('Ozols', 'Jānis'): {
'Math': '10',
'ProgVal': '5',
'Sports': '5'
},
('Krumiņa', 'Ilze'): {
'Math': '7',
'ProgVal': '3',
'Sports': '6'
},
('Liepa', 'Peteris'): {
'Math': '3',
'ProgVal': '7',
'Sports': '7'
},
('Lapsa', 'Maris'): {
'Math': '10',
'ProgVal': '10',
'Sports': '3'
}
}
courses = ['Math', 'ProgVal', 'Sports']
def fail():
for k,v in students.items():
for course, grade in v.items():
if int(grade) < 4:
print(f'{k[0]} {k[1]} failed in {course}')
fail()
output输出
Krumiņa Ilze failed in ProgVal
Liepa Peteris failed in Math
Lapsa Maris failed in Sports
There're a few issues with your code.您的代码存在一些问题。
Firstly, you should be using an integer (int) or float to store a student's score.首先,您应该使用整数 (int) 或浮点数来存储学生的分数。 You can fix it by changing them to integers:您可以通过将它们更改为整数来修复它:
students = {('Ozols', 'Jānis'): {'Math': 10, 'ProgVal': 5, 'Sports': 5},
('Krumiņa', 'Ilze'): {'Math': 7, 'ProgVal': 3, 'Sports': 6},
('Liepa', 'Peteris'): {'Math': 3, 'ProgVal': 7, 'Sports': 7},
('Lapsa', 'Maris'): {'Math': 10, 'ProgVal': 10, 'Sports': 3}}
Secondly, you should not be using a loop of students.values()
within a loop of students.keys()
as it will loop through the entire dictionary again - you want to loop through each subject of the values (which are dictionaries of scores) of each student , which is stored in the list called students
.其次,你不应该使用的循环students.values()
的循环中students.keys()
因为它会遍历整个字典再次-你通过值(这是分数的字典的每个科目要循环)的每个学生,它存储在名为students
的列表中。
Try this:尝试这个:
students = {('Ozols', 'Jānis'): {'Math': 10, 'ProgVal': 5, 'Sports': 5},
('Krumiņa', 'Ilze'): {'Math': 7, 'ProgVal': 3, 'Sports': 6},
('Liepa', 'Peteris'): {'Math': 3, 'ProgVal': 7, 'Sports': 7},
('Lapsa', 'Maris'): {'Math': 10, 'ProgVal': 10, 'Sports': 3}}
courses = ['Math', 'ProgVal', 'Sports']
for lName, fName in students.keys():
studentRecord = students[(lName, fName)]
for course in studentRecord:
if studentRecord[course] < 4:
print(fName, lName, course)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.