【问题标题】:Is there a way to insert into numpy array in a loop?有没有办法在循环中插入 numpy 数组?
【发布时间】:2020-02-20 20:40:59
【问题描述】:

我有一个 2D numpy 数组:

[[9.29526424407959, -3.68755626678467],
 [9.7620153427124, -2.16865086555481], 
 [9.9980001449585, 0.199986666440964], 
 [9.95050621032715, 0.993697226047516], 
 [9.84010124206543, 1.78112530708313], 
 [9.43374633789063, 3.31729197502136], 
 [8.7891960144043, 4.76969909667969], 
 [8.38245868682861, 5.4529242515564],
 [7.41290092468262, 6.71184778213501],
 [6.85620975494385, 7.27958679199219],
 [5.61658048629761, 8.27369403839111],
 [4.23513603210449, 9.05889701843262],
 [3.50201725959778, 9.36674308776855]]

我需要在它的开头添加数字使其看起来像这样。

[[1 ,9.29526424407959, -3.68755626678467],
 [2, 9.7620153427124, -2.16865086555481], 
 [3, 9.9980001449585, 0.199986666440964], 
 [4, 9.95050621032715, 0.993697226047516], 
 [5, 9.84010124206543, 1.78112530708313], 
 [6, 9.43374633789063, 3.31729197502136], 
 [7, 8.7891960144043, 4.76969909667969], 
 .
 .
 .

我尝试将其转换为列表并添加数字,但总是出错。

有什么建议吗?

【问题讨论】:

标签: python python-2.7 numpy


【解决方案1】:
your_array = [[9.29526424407959, -3.68755626678467],
 [9.7620153427124, -2.16865086555481], 
 [9.9980001449585, 0.199986666440964], 
 [9.95050621032715, 0.993697226047516], 
 [9.84010124206543, 1.78112530708313], 
 [9.43374633789063, 3.31729197502136], 
 [8.7891960144043, 4.76969909667969], 
 [8.38245868682861, 5.4529242515564],
 [7.41290092468262, 6.71184778213501],
 [6.85620975494385, 7.27958679199219],
 [5.61658048629761, 8.27369403839111],
 [4.23513603210449, 9.05889701843262],
 [3.50201725959778, 9.36674308776855]]

your_array = np.array(your_array)
index = np.array(range(1,len(your_array)+1))
np.concatenate((index.reshape(-1,1),your_array),axis=-1)

输出:

array([[ 1.        ,  9.29526424, -3.68755627],
       [ 2.        ,  9.76201534, -2.16865087],
       [ 3.        ,  9.99800014,  0.19998667],
       [ 4.        ,  9.95050621,  0.99369723],
       [ 5.        ,  9.84010124,  1.78112531],
       [ 6.        ,  9.43374634,  3.31729198],
       [ 7.        ,  8.78919601,  4.7696991 ],
       [ 8.        ,  8.38245869,  5.45292425],
       [ 9.        ,  7.41290092,  6.71184778],
       [10.        ,  6.85620975,  7.27958679],
       [11.        ,  5.61658049,  8.27369404],
       [12.        ,  4.23513603,  9.05889702],
       [13.        ,  3.50201726,  9.36674309]])

【讨论】:

    【解决方案2】:

    您可以使用 np.insert 将值附加到您的 numpy 数组。

     import numpy as np
    
     a = [[9.29526424407959, -3.68755626678467],
          [9.7620153427124, -2.16865086555481], 
          [9.9980001449585, 0.199986666440964], 
          [9.95050621032715, 0.993697226047516], 
          [9.84010124206543, 1.78112530708313], 
          [9.43374633789063, 3.31729197502136], 
          [8.7891960144043, 4.76969909667969], 
          [8.38245868682861, 5.4529242515564],
          [7.41290092468262, 6.71184778213501],
          [6.85620975494385, 7.27958679199219],
          [5.61658048629761, 8.27369403839111],
          [4.23513603210449, 9.05889701843262],
          [3.50201725959778, 9.36674308776855]]
    
     index = 0
     values = range(1, len(a)+1)
     b = np.insert(a, index, values, axis=1) 
    
     >>> array([[ 1.        ,  9.29526424, -3.68755627],
                [ 2.        ,  9.76201534, -2.16865087],
                [ 3.        ,  9.99800014,  0.19998667],
                [ 4.        ,  9.95050621,  0.99369723],
                [ 5.        ,  9.84010124,  1.78112531],
                [ 6.        ,  9.43374634,  3.31729198],
                [ 7.        ,  8.78919601,  4.7696991 ],
                [ 8.        ,  8.38245869,  5.45292425],
                [ 9.        ,  7.41290092,  6.71184778],
                [10.        ,  6.85620975,  7.27958679],
                [11.        ,  5.61658049,  8.27369404],
                [12.        ,  4.23513603,  9.05889702],
                [13.        ,  3.50201726,  9.36674309]])
    

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2021-12-21
      • 2021-02-04
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多