【问题标题】:how to turn this python lines code into a list comprehension?如何将此 python 行代码转换为列表理解?
【发布时间】:2020-03-12 15:27:53
【问题描述】:

我被要求将这些行转换为列表理解

tmaxima = []
ag_maxima = []
for i in sorted_indices:
tmaxima.append(t[i])
ag_maxima.append(a_g[i])

【问题讨论】:

  • (1) 缩进在语法上无效。 (2) 您将需要两个列表推导。 (3) 展示您自己解决问题的努力和代码(问题中格式正确的文本)。

标签: python list for-loop list-comprehension spyder


【解决方案1】:

假设sorted_indices 只包含整数:

tmaxima = [t[i] for i in sorted_indices]
ag_maxima = [a_g[i] for i in sorted_indices]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    相关资源
    最近更新 更多