[Python.NET] converting double array to numpy list

Jeffrey Bush jeff at coderforlife.com
Wed Aug 20 21:23:36 CEST 2014

Numpy arrays are almost always created in Python and there are dozens of
methods. That post you link to has the best solution for this case:

import numpy as np
dest = np.empty(len(src))

That creates a new numpy array of the same length of src with
undefined ("empty") values at every index. You could substitute len(src)
for whatever length you want (or a tuple if you want something not 1D). We
use empty since we are about to set every value to something so we don't
care that they have undefined values at that point.

Overall I don't understand your problem, can you be more clear? I don't
know if you are trying to do the copy in Python or C#, I am guessing you
are copying from C# array to a new Python NumPy array, which is exactly
what that post did (in Python).


On Wed, Aug 20, 2014 at 10:21 AM, Mika S <siddhupiddu at gmail.com> wrote:

> How do I pass a double array as a numpy list from C# (calling python code
> from c#) or passing something that can become a numpy list with minimal
> overhead.
> Right now, the naive way would be to create a new PyList. And to fill it
> with elements from the double array.
> But I read on a thread on this list (
> https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) that
> Marshal.copy was faster.
> So, I tried Marshal.copy but I can't figure out how to instantiate a numpy
> array in python. Numpy can instantiate a new array from native C array by
> calling numpy.ctypeslib.as_array.
> So, theoretically if i can do
> Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the
> addrofnative (which is a IntPtr) I could be done. But I can only pass
> PyObject when calling a module function.
> Any ideas ?
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> https://mail.python.org/mailman/listinfo/pythondotnet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20140820/e1490992/attachment.html>

More information about the PythonDotNet mailing list