相关疑难解决方法(0)

如何使用函数numpy.append

我使用函数numpy.append时遇到问题.我将以下函数编写为更大的代码片段的一部分,但是,我的错误在以下内容中重现:

data = [
         [
          '3.5', '3', '0', '0', '15', '6', 
          '441', 'some text', 'some more complicated data'
         ], 
         [
          '4.5', '5', '1', '10', '165', '0', 
          '1', 'some other text', 'some even more complicated data'
         ]
       ]

def GetNumpyArrey(self, index):
    r = np.array([])
    for line in data:
        np.append(r, float(line[index]))

    print r
Run Code Online (Sandbox Code Playgroud)

index <6.结果是:

>> []
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

非常感谢 !

python numpy append

17
推荐指数
1
解决办法
2万
查看次数

'索引0超出轴0的大小为0'是什么意思?

我是python和numpy的新手.我运行了一个我编写的代码,我收到了这条消息:'索引0超出了0号轴的大小为0'没有上下文,我只是想弄明白这意味着什么..问这个可能很傻但它们的轴0和尺寸0是什么意思?index 0表示数组中的第一个值..但我无法弄清楚0和0的意思.

'data'是一个文本文件,在两列中有许多数字.

x = np.linspace(1735.0,1775.0,100)
column1 = (data[0,0:-1]+data[0,1:])/2.0
column2 = data[1,1:]
x_column1 = np.zeros(x.size+2)
x_column1[1:-1] = x
x_column1[0] = x[0]+x[0]-x[1]
x_column1[-1] = x[-1]+x[-1]-x[-2]
experiment = np.zeros_like(x)
for i in range(np.size(x_edges)-2):
    indexes = np.flatnonzero(np.logical_and((column1>=x_column1[i]),(column1<x_column1[i+1])))
    temp_column2 = column2[indexes]
    temp_column2[0] -= column2[indexes[0]]*(x_column1[i]-column1[indexes[0]-1])/(column1[indexes[0]]-column1[indexes[0]-1])
    temp_column2[-1] -= column2[indexes[-1]]*(column1[indexes[-1]+1]-x_column1[i+1])/(column1[indexes[-1]+1]-column1[indexes[-1]])
    experiment[i] = np.sum(temp_column2)   
return experiment
Run Code Online (Sandbox Code Playgroud)

python indexing error-handling numpy index-error

7
推荐指数
3
解决办法
4万
查看次数

标签 统计

numpy ×2

python ×2

append ×1

error-handling ×1

index-error ×1

indexing ×1