473,383 Members | 1,818 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,383 software developers and data experts.

Passing Arrays (maybe lists) from Python to C

This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to do, but because I can't seem to locate something that is even close... I am posting... I would like to thank this community, I have received a great deal of help already.

Background: I have a pre-existing C code, that I have written, which works. The only problem with it is it is slow. I have decided to rewrite the two control functions from C to python. I have succeeded in getting most everything set up, but I am having trouble with, what seems to me, very simple things.

In my C code I declare all of my arrays, I do most everything in arrays, up front, and I have preserved that in my Python. I am using c_types and numpy.

Here are some of my array declarations:
Expand|Select|Wrap|Line Numbers
  1. pop_size = gens = ... = c_int
  2. rank = (c_int * 500)()
  3. ...
  4. p1_chromo = p2_chromo = ... = (c_float * 500)()
  5. population = []
  6.  
The "population" array needs to be a 2D array and is initialized like this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(pop_size)
  2.      population.append([])
  3.      for j in range(n)
  4.          population[i].append(0.0)
  5.  
I am initializing it this way, because if I used the numpy function "zeros" to return a 2D array full of zeros I had trouble passing it.

Then I go on down and I do various things to the population and all seems to work well. Then i need to pass PART of my 2D population array to a C function.
it looks like this:
Expand|Select|Wrap|Line Numbers
  1. encode(n,population[x1], population[x2], p1_chromo, p2_chromo)
  2.  
The C function prototype is:
Expand|Select|Wrap|Line Numbers
  1. void encode(int n, float pop1[], float pop2[], float p1_chromo[], float p2_chromo[])
  2.  
The error that I get, all the time it seems, is ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to convert parameter 2

I understand what the error is telling me. I am trying to put a list into an array of floats. I over came this problem in previous function calls, when I was passing 1D arrays, by using the byref(...) tag, flag or whatever it is called.

I cannot understand a good way to create indexible arrays in python and then pass them to ANSI C funtions to be worked on. Thats my question... A mountain of text for that little line. But I wanted to show what I was doing and where I was coming from. I have read most of numpy's documentation, and pretty much all of the forum posts I could find... Any suggestions would be greatly appreciated... THANKS!!!
Nov 12 '08 #1
1 4533
I have fixed it.... yay!
Nov 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mb3242 | last post by:
Hello, I'm trying to use win32com to call a method in a COM object. I'm having a problem with Python choosing the wrong type when wrapping up lists into VARIANTs. The function I'm trying to call...
11
by: Soeren Sonnenburg | last post by:
Hi all, Just having started with python, I feel that simple array operations '*' and '+' don't do multiplication/addition but instead extend/join an array: a= >>> b= >>> a+b
3
by: Kermit Rose | last post by:
From: Kermit Rose Date: 02/10/06 17:36:34 To: python-help@python.org Subject: Arrays Hello. I want to write a program in python using integer arrays.
4
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
11
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
11
by: Gordon C | last post by:
Absolute newbie here. In spite of the Python Software Foundation tutorial's ( http://www.python.org/doc/current/tut/tut.html ) use of the array declaration array(type), the Python interpreter does...
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?

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.