By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,508 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,508 IT Pros & Developers. It's quick & easy.

Converting a c array to python list

P: n/a
Hi!

I want to embed a function in my python application, that creates a
two-dimensional array of integers and passes it as a list (preferably a
list of lists, but that is not necessary, as the python function knows
the dimensions of this array). As I read the reference, I see, that I
must first initialize a list object and then item-by-item put the values
to the list. Is there any faster way to do it? And is it worth to
implement? The same problem is resolved in the current version by
calling a smaller c function (that counts just one element of the array)
many times. Will it add much performance to the process?

zefciu
Mar 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
zefciu wrote:
Hi!

I want to embed a function in my python application, that creates a
two-dimensional array of integers and passes it as a list (preferably a
list of lists, but that is not necessary, as the python function knows
the dimensions of this array). As I read the reference, I see, that I
must first initialize a list object and then item-by-item put the values
to the list. Is there any faster way to do it? And is it worth to
implement? The same problem is resolved in the current version by
calling a smaller c function (that counts just one element of the array)
many times. Will it add much performance to the process?

zefciu
http://members.dsl-only.net/~daniels/Block.html

This will allow you to provide a python "View" onto your C data.
The view can be cut into a list of views as on_way or other_way below:
...
view = block.View(...)
stride = <<one dimension>>
one_way = [view[n: n + stride] for n in range(0, len(view), stride)]
other_way = [view[n::stride] for n in range(stride)]
...
One of these will give you row-major, and the other column-major access
to the live data in the C array. If you don't want to see your program
data as it changes, you could Create a Block and fill it. If you need
Python 2.4 or 2.5, you'll need to figure out how to build from sources
on Windows (I assume building from sources is otherwise "easy").

--
--Scott David Daniels
sc***********@acm.org
Mar 2 '07 #2

P: n/a
I have just read about buffer and array objects and I think one of them
could be fit for my need. However there are two questions.

If i make a buffer from a part of dynamically allocated memory, what
would free it? Should it be allocated with malloc or some
python-specific function?

How on earth can I create array object in C?

zefciu
Mar 2 '07 #3

P: n/a
In article <es**********@inews.gazeta.pl>,
zefciu <ze*****@Speacock.Pau.Apoznan.Mplwrote:
Hi!

I want to embed a function in my python application, that creates a
two-dimensional array of integers and passes it as a list (preferably a
list of lists, but that is not necessary, as the python function knows
the dimensions of this array). As I read the reference, I see, that I
must first initialize a list object and then item-by-item put the values
to the list. Is there any faster way to do it? And is it worth to
implement? The same problem is resolved in the current version by
calling a smaller c function (that counts just one element of the array)
many times. Will it add much performance to the process?
My first thought is to use the numpy library since it is good at quickly
creating large arrays (of any dimension) and you can easily get the data
out as a list if you really need that (but are you sure you need that?
You may be able to just use the numpy array directly).

It might help to have a clearer idea of why you want to do this.

-- Russell

P.S. numarray or Numeric would also do the job. They are older,
deprecated numeric libraries. numpy is recommended for new code.
Mar 2 '07 #4

P: n/a
Russell E. Owen wrote:
>
It might help to have a clearer idea of why you want to do this.
I am writing a Mandelbrot fractal generator with Tkinter interface. Now
the generation works like this - there is a loop in python which
iterates through fractal's pixels and for each of them calls a functions
which checks if it belongs to Mandelbrot set or how many iterations does
it take for this point to bail out. Then the python function sets the
pixel's colour according to the result.

I was testing it first with python function, written rather for
readibility not speed, using the builtin complex class. It took about
90 s to generate a 1152x864 fractal. Then I used a c function and it
took 14 s. When I told it to my friend (grad student of informatics) he
said "And probably 90% of the time takes calling the function".

So I think, that maybe I should put the whole loop into c code. But as
before the function returned only one integer at every call, now it
would have to return a rather large array of integers (maybe chars would
be enough).

zefciu
Mar 3 '07 #5

P: n/a
Dennis Lee Bieber wrote:
Written properly, all it returns is the address of that array data
-- there is no massive copying of data..
I know :) That's why I want to know how to write it properly.

zefciu
Mar 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.