繁体   English   中英

从二维列表中获取值

[英]getting a value from 2d list

我正在尝试使用嵌套列表理解从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”“输出为['Harry',37.21]”

您不需要对此有嵌套的理解。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM