<分区>
我有以下代码
last_question = max(answer.question_id for answer in answers)
我不仅需要检索最大的 question_id,还需要检索包含此 question_id
的 answer
对象。我可以用下一个代码来完成(假设 answers
非空):
max_answer = answers[0]
for answer in answers:
if answer.question_id > max_answer.question_id:
max_answer = answer
last_question = max_answer.question_id
但是我可以用 Python 的方式缩短它吗?