程序本身要求可以根据"grade"进行排列
students = [
{"sno":101,"name":"小张","grade":88},
{"sno":102,"name":"小王","grade":90},
{"sno":103,"name":"小李","grade":108},
{"sno":104,"name":"小赵","grade":68},
]
student_sort = sorted(students,key=lambda X:X["grade"])
print(student_sort)
#尝试令lambda X:X["grade"]等价于students["grad]"]#
student_sort_02 = sorted(students,key=students["grad"])
print(student_sort_02)
#运行后报错证明完全错误#
#错误代码TypeError: list indices must be integers or slices, not str#
对于题中lambda的理解如下:
#student_sort = sorted(students,key=lambda X:X["grade"]))
lambda X:X["grade"]可以认为设置了一个X作为临时变量
问题如下:
1,X["grade"]可能解释为 遍历所有字典的["grade"] ,这种理解是否正确?若错误正确的解释方式是?