【问题标题】:Numpy array (list of lists), add values at the endNumpy数组(列表列表),在末尾添加值
【发布时间】:2020-10-14 22:28:07
【问题描述】:

我有两个数组:

In [32]: a                                                                      
Out[32]: 
array([[1, 2, 3],
       [2, 3, 4]])

In [33]: b                                                                      
Out[33]: 
array([[ 8,  9],
       [ 9, 10]])

我想得到以下:

In [35]: c                                                                      
Out[35]: 
array([[ 1,  2,  3,  8,  9],
       [ 2,  3,  4,  9, 10]])

即将b[0] = array([8, 9]) 的第一个和第二个值作为a[0] 的最后两个值 并将b[1] = array([9,10]) 的第一个和第二个值附加为a[1] 的最后两个值。

此链接中的第二个答案:How to add multiple extra columns to a NumPy array 不起作用,我不明白接受的答案。

【问题讨论】:

    标签: python arrays numpy append


    【解决方案1】:

    你可以试试np.hstack:

    a=np.array([[1, 2, 3],
               [2, 3, 4]])
        
    
    b=np.array([[ 8,  9],
           [ 9, 10]])
    print(np.hstack((a,b)))
    

    输出:

    [[ 1  2  3  8  9]
     [ 2  3  4  9 10]]
    

    或者由于您附加的链接的第一个答案比concatenate快,并且您可以看到G.Anderson的时间,最快的是连接,这里有一个解释,所以您可以使用that first answer

    #So you create an array of the same shape that the expected concatenate output:
    res = np.zeros((2,5),int)
    res
    [[0 0 0 0 0]
     [0 0 0 0 0]]
    
    #Then you assign res[:,:3] to fisrt array, where res[:,:3] that is the first 3 elements of each row
    res[:,:3]
    [[0 0 0]
     [0 0 0]]
    res[:,:3]=a   #assign
    res[:,:3]
    [[1, 2, 3]
     [2, 3, 4]]
    
    
    #Then you assign res[:,3:] to fisrt array, where res[:,3:] that is the last two elements of eah row
    res[:,3:]
    [[0 0]
     [0 0]]
    res[:,3:]=b  #assign
    res[:,3:]
    [[ 8,  9]
     [ 9, 10]]
    
    #And finally:
    res
    [[ 1  2  3  8  9]
     [ 2  3  4  9 10]]
    

    【讨论】:

      【解决方案2】:

      你可以concatenate:

      np.concatenate([a,b], axis=1)
      

      输出:

      array([[ 1,  2,  3,  8,  9],
             [ 2,  3,  4,  9, 10]])
      

      【讨论】:

        【解决方案3】:

        您可以使用np.append 和轴参数来连接给定轴上的两个数组

        np.append(a,b, axis=1)
        
        array([[ 1,  2,  3,  8,  9],
               [ 2,  3,  4,  9, 10]])
        

        为了完整起见,添加前三个答案的时间。请注意,这些时间会因运行代码的机器而异,并且可能会针对不同大小的数组以不同的速率进行扩展

        %timeit np.append(a,b, axis=1)
        2.81 µs ± 438 ns per loop
        
        %timeit np.concatenate([a,b], axis=1)
        2.32 µs ± 375 ns per loop
        
        %timeit np.hstack((a,b))
        4.41 µs ± 489 ns per loop
        

        【讨论】:

        • 我不鼓励使用np.append。将其视为列表追加克隆太诱人了,但事实并非如此。
        【解决方案4】:

        来自关于 numpy.concatenate

        的 numpy 文档

        沿现有轴加入一系列数组。

        从问题中,我明白你想要什么

        import numpy as np
        
        a = np.array([[1, 2, 3],
               [2, 3, 4]])
        
        b = np.array([[ 8,  9],
               [ 9, 10]])
        
        c = np.concatenate((a, b), axis=1)
        
        print ("a: ", a)
        print ("b: ", b)
        print ("c: ", c)
        

        输出:

        a:  [[1 2 3]
         [2 3 4]]
        
        b:  [[ 8  9]
         [ 9 10]]
        
        c:  [[ 1  2  3  8  9]
         [ 2  3  4  9 10]]
        

        【讨论】:

          猜你喜欢
          • 2021-04-24
          • 2019-09-17
          • 2014-04-20
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多