如何找到二维数组的最小分数?
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]
作为键。
阅读 official documentation 以了解 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]]
如何找到 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]
作为键。
阅读 official documentation 以了解 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]]