python append函数,如何使用函数numpy.append

在Python中,使用numpy.append时需要注意它不会就地修改数组,而是返回一个新的数组。该博客中提到的问题是由于没有正确赋值返回的结果导致的。更高效的方法是先构建Python列表,然后一次性转换为numpy数组。这可以避免多次操作带来的效率损失。
摘要由CSDN通过智能技术生成

I have a problem using the function numpy.append.

I wrote the following function as part of a larger piece of code,

however, my error is reproduced in the folowing:

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

index < 6.

the result is:

>> []

what am I doing wrong?

Thanks a lot !

解决方案

Unlike the list append method, numpy's append does not append in-place. It returns a new array with the extra elements appended. So you'd need to do r = np.append(r, float(line[index])).

Building up numpy arrays in this way is inefficient, though. It's better to just build your list as a Python list and then make a numpy array at the end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值