【问题标题】:Is there a way of unzipping a list of nested redundant list?有没有办法解压缩嵌套冗余列表的列表?
【发布时间】:2020-11-19 07:44:10
【问题描述】:

我有这个清单:

input = [[[1,2]], [[3,4]], [[5,6]]]

想要的输出:

output = [[1,3,5],[2,4,6]]

我试过这个:

x, y = map(list,zip(*input))

后来意识到由于多余的方括号,此方法不起作用, 有没有办法在不迭代的情况下解决这个问题。

【问题讨论】:

    标签: python-3.x list-manipulation


    【解决方案1】:

    你可以试试这个:

    >>> from operator import itemgetter
    >>> input = [[[1,2]], [[3,4]], [[5,6]]]
    >>> list(zip(*map(itemgetter(0), input)))
    [(1, 3, 5), (2, 4, 6)]
    

    【讨论】:

      【解决方案2】:
      In [117]: input = [[[1,2]], [[3,4]], [[5,6]]]
      
      In [118]: list(zip(*[i[0] for i in input]))
      Out[118]: [(1, 3, 5), (2, 4, 6)]
      
      In [119]: list(map(list, zip(*[i[0] for i in input])))
      Out[119]: [[1, 3, 5], [2, 4, 6]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        相关资源
        最近更新 更多