我还在学习python,我在从字典中提取数据时遇到了一些问题。我需要创建一个循环来检查每个值并提取关键字。因此,对于这段代码,我需要找到优秀的学生。我被卡在了第三行#空白。我该怎么做呢?提前感谢
class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
for student in class:
if #blank:
print("Hello, "+student+" students!")
else:
print("odd")
发布于 2020-04-20 11:15:59
使用字典方法"keys(),values(),items()":
def get_students_by_criteria(student_class, criteria):
students = []
for candidate, value in student_class.items():
if value == criteria:
students.append(candidate)
return students
my_class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
print(get_students_by_criteria(my_class, "nice"))
"class“一词的警告:它是为面向python编程的对象保留的关键字。
https://stackoverflow.com/questions/61320741
复制相似问题