代码之家  ›  专栏  ›  技术社区  ›  Mod_x

如何将多行列表转换为单行列表?[重复]

  •  0
  • Mod_x  · 技术社区  · 4 年前
    [[0, 0, 3, 50], [50, 100, 4, 20]]
    

    我想把上面的列表转换成,

    [0, 0, 3, 50, 50, 100, 4, 20].
    

    我试过使用split()但不起作用。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Tin Stribor Sohn    4 年前

    尝试以下方法:

    list_2d = [[0, 0, 3, 50], [50, 100, 4, 20]]
    list_1d = [i for nested in list_2d for i in nested]
    print(list_1d)