【问题标题】:How to create one list with selected elements from another [duplicate]如何使用另一个列表中的选定元素创建一个列表[重复]
【发布时间】:2016-09-11 12:44:33
【问题描述】:

我有清单:

[[(5, 0.13074668991267904), (7, 0.50480936593447379), (11, 0.24881894415284611)]
[(19, 0.2984135829981458), (24, 0.31341486626464521), (29, 0.15972991970724332), (31, 0.12844163102996534)]]

我需要使用前一个列表中的第二个值创建一个新列表,该列表必须如下所示:

[[0.13074668991267904, 0.50480936593447379, 0.24881894415284611)]
 [0.2984135829981458, 0.31341486626464521, 0.15972991970724332), 0.12844163102996534)]]

我该怎么做?

【问题讨论】:

    标签: python arrays list multidimensional-array


    【解决方案1】:

    您可以遍历原始列表,将每个列表中的最后一个元素作为一个选项。

    new_list = [[x[-1] for x in a_list] for a_list in nested_lists]
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      相关资源
      最近更新 更多