473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Converting a c array to python list

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
5 2315
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Foster | last post by:
I have some code which attempts to convert Python arrays (tuples of tuples of tuples...) etc. into C arrays with equivalent contents. The prototype code is shown below. My only question is, is...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
5
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the...
5
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence....
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
8
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project...
11
by: Louis.Soninhu | last post by:
Hi pals I have a list like this mylist= I'd like to remove the first and the last item as they are irrevalent, and convert it to the dict: {'tom':'boss','mike':'manager','paul':'employee'}
10
by: J. Peng | last post by:
what's the difference between an array and a list in python? I see list has all features of array in C or perl. so please tell me.thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.