Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return numpy array from python to Lazarus#488

Unanswered
Chemistza74 asked this question inQ&A
Discussion options

Hi,

I've recently started playing around with P4D in Lazarus/FPC. I'm trying to read a numpy array created in python back in to FPC. The python code which I have in SynEdit1:

import numpy as nppy_array = np.zeros(10)for i in range (10):    py_array[i]=i

My attempt at the FPC code below to extract py_array is based on FPC Demo35:

const  N = 10;type  PIntArray = ^TIntArray;  TIntArray = array[0..N - 1] of NativeInt;  procedure TForm1.Button1Click(Sender: TObject);var  np_arr: PPyObject;  PyBuffer: Py_buffer;  I:integer;  res:int64;begin  self.PythonEngine1.ExecStrings(self.SynEdit1.lines);  np_arr := ExtractPythonObjectFrom(MainModule.py_array);  PythonEngine1.PyObject_GetBuffer(np_arr, @PyBuffer, PyBUF_CONTIG);  PythonEngine1.CheckError;  try    for I := 0 to N - 1 do      res:= PIntArray(PyBuffer.buf)^[I];      self.ListBox1.Items.Add(inttostr(res));  finally    PythonEngine1.PyBuffer_Release(@PyBuffer);  end;end;

However, when I run this, I only end up with a single, random number in ListBox1. Any suggestions on how to resolve this would be greatly appreciated!

You must be logged in to vote

Replies: 1 comment

Comment options

If you run this code in PyScripter

importnumpyasnppy_array=np.zeros(10)foriinrange (10):py_array[i]=iprint(py_array)

The output is:

[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

Notice that the values are floating point and you try to read them as Integers.

I think if you change your code to:

importnumpyasnppy_array=np.zeros(10,dtype=np.int32)foriinrange (10):py_array[i]=i

or simpler:

importnumpyasnppy_array=np.array(range(10))

it should work.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Chemistza74@pyscripter

[8]ページ先頭

©2009-2025 Movatter.jp