【问题标题】:Python: first element in a nested listPython:嵌套列表中的第一个元素
【发布时间】:2014-11-06 12:48:59
【问题描述】:

我想要一个仅包含嵌套列表的第一个元素的列表。 嵌套列表 L,它看起来像:

L =[ [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]] ]

for l in L:
 for t in l:
  R.append(t[0])
print 'R=', R

输出是R= [0, 3, 6, 0, 3, 6, 0, 3, 6],但我想得到一个单独的结果,例如:

R= [[0, 3, 6], [0, 3, 6], [0, 3, 6]]

我也尝试过像[[R.append(t[0]) for t in l] for l in L] 这样的列表理解,但这给出了[[None, None, None], [None, None, None], [None, None, None]]

怎么了?

【问题讨论】:

    标签: python list python-2.7 list-comprehension


    【解决方案1】:

    你可以这样做:

    >>> L = [ [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]] ]
    >>> R = [ [x[0] for x in sl ] for sl in L ]
    >>> print R
    [[0, 3, 6], [0, 3, 6], [0, 3, 6]]
    

    【讨论】:

      【解决方案2】:
      L =[ [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]] ]
      R=[]
      for l in L:
       temp=[]
       for t in l:
        temp.append(t[0])
       R.append(temp)
      print 'R=', R
      

      输出:

      R= [[0, 3, 6], [0, 3, 6], [0, 3, 6]]
      

      【讨论】:

        【解决方案3】:

        您希望输出为嵌套列表。您可以像这样“手动”嵌套它们:

        L =[ [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]] ]
        R = []
        for l in L:
            R2 = []
            for t in l:
                r2.append(t[0])
            R.append(R2)
        print 'R=', R
        

        【讨论】:

          【解决方案4】:

          您的解决方案返回 [[None, None, None], [None, None, None], [None, None, None]],因为方法 append 返回值 None。将其替换为 t[0] 应该可以解决问题。

          您正在寻找的是:

          R = [[t[0] for t in l] for l in L]
          

          【讨论】:

            【解决方案5】:

            您也可以使用带有转置功能的 numpy 数组。

            import numpy as np
            L = [ [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]], [[0,1,2],[3,4,5],[6,7,8]] ]
            Lnumpy = np.array(L)
            Ltransposed = Lnumpy.transpose(0, 2, 1) # Order of axis
            

            现在是输出

            [[[0 3 6]
              [1 4 7]
              [2 5 8]]
            
             [[0 3 6]
              [1 4 7]
              [2 5 8]]
            
             [[0 3 6]
              [1 4 7]
              [2 5 8]]]
            

            现在您不需要 member 的每个第一个成员,而只需要第一个成员。

            print(Ltransposed[0][0]) 现在给你[0, 3, 6] 那么

            for i in ltr:
                print(ltr[0][0])
            

            输出

            [0 3 6]
            [0 3 6]
            [0 3 6]
            

            只是为了细节,也有可能使用 zip...(这里是 Python 3...)

            print(list(zip(*Ltransposed))[0])
            

            给你同样的。如果您需要列表,请将其转换回来...list()...

            【讨论】:

              猜你喜欢
              • 2018-05-30
              • 2013-12-24
              • 1970-01-01
              • 1970-01-01
              • 2021-04-21
              • 1970-01-01
              • 1970-01-01
              • 2021-03-11
              • 2023-03-22
              相关资源
              最近更新 更多