从二维列表中获取值

时间:2019-05-23 20:10:41

标签: python list-comprehension

我正在尝试使用嵌套列表理解从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]”

1 个答案:

答案 0 :(得分:1)

您不需要嵌套的理解。

result = [student[1] for student in students if student[1] == unique_scores[1]]