如何将数据从 numpy 数组复制到另一个数组 [英] How to copy data from a numpy array to another
本文介绍了如何将数据从 numpy 数组复制到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在不修改数组a的地址的情况下,将数据从数组b复制到数组a的最快方法是什么?我需要这个是因为外部库 (PyFFTW) 使用了一个指向我的数组的指针,该指针无法更改.
What is the fastest way to copy data from array b to array a, without modifying the address of array a. I need this because an external library (PyFFTW) uses a pointer to my array that cannot change.
例如:
a = numpy.empty(n, dtype=complex)
for i in xrange(a.size):
a[i] = b[i]
可以不用循环吗?
推荐答案
我相信
a = numpy.empty_like(b)
a[:] = b
将快速复制值.正如 Funsi 提到的,最近版本的 numpy 也有 copyto
功能.
will copy the values quickly. As Funsi mentions, recent versions of numpy also have the copyto
function.
这篇关于如何将数据从 numpy 数组复制到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文