Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法在循环中插入numpy数组?_Python_Python 2.7_Numpy - Fatal编程技术网

Python 有没有办法在循环中插入numpy数组?

Python 有没有办法在循环中插入numpy数组?,python,python-2.7,numpy,Python,Python 2.7,Numpy,我有一个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.31729197

我有一个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], 
 .
 .
 .
我试着将它转换成列表并添加数字,但结果总是出错

有什么建议吗

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]])
可以使用将值附加到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]])

这回答了你的问题吗?