我正在尝试使用嵌套列表理解从 2d 列表中提取一个值,但是我无法这样做。它会输出一个包含 2 个值的列表。有可能实现吗?
unique_scores = [37.2, 37.21, 39, 41]
students = [['Harry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
result = [score for student in students for score in student if student[1] == unique_scores[1]]
print(result)
"预期:哈利" "输出是 ['Harry', 37.21]"
您不需要对此进行嵌套理解。
result = [student[1] for student in students if student[1] == unique_scores[1]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句