[英]how to find minimum score of a 2d array?
如何找到2d array
最小分数。
我的数组就像:
[['john', 20], ['jack', 10], ['tom', 15]]
我想找到学生的最低分数并print
他的名字。 告诉我该怎么写?
如果您只想招收一名学生:
student_details = [['john', 20], ['jack', 10], ['tom', 15]]
student_detail_with_min_score = min(student_details, key=lambda detail: detail[1])
print(student_detail_with_min_score)
print(student_detail_with_min_score[0])
print(student_detail_with_min_score[1])
输出:
['jack', 10]
jack
10
min
函数查找student_details
的最小值,但在比较时将使用student_details[i][1]
作为键。
阅读官方文档以了解min
函数如何与key
参数一起使用。
如果要让所有学生获得最低分数:
student_details = [['rock', 10], ['john', 20], ['jack', 10], ['tom', 15]]
min_score = min(student_details, key=lambda detail: detail[1])[1]
student_details_with_min_score = [
student_detail for student_detail in student_details if student_detail[1] == min_score
]
print(student_details_with_min_score)
输出:
[['rock', 10], ['jack', 10]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.