2

I am a Python n00b and at the risk of asking an elementary question, here I go.

I am porting some code from C to Python for various reasons that I don't want to go into.

In the C code, I have some code that I reproduce below.

float table[3][101][4];
int kx[6] = {0,1,0,2,1,0};
int kz[6] = {0,0,1,0,1,2};

I want an equivalent Python expression for the C code below:

float *px, *pz;
int lx = LX; /* constant defined somewhere else */
int lz = LZ; /* constant defined somewhere else */
px = &(table[kx[i]][0][0])+lx;
pz = &(table[kz[i]][0][0])+lz;

Can someone please help me by giving me the equivalent expression in Python?

2
  • 1
    If you wrap whatever you need in structs you can just use them from python directly instead of trying to implement low level functionality in a high level language which is a recipe for tears Commented Jul 18, 2013 at 15:27
  • This first code block is easy to convert but the second is looks scary - what do you do with px and pz? Commented Jul 18, 2013 at 15:39

2 Answers 2

3

Here's the thing... you can't do pointers in python, so what you're showing here is not "portable" in the sense that:

float *px, *pz;    <-- this doesn't exist
int lx = LX; /* constant defined somewhere else */
int lz = LZ; /* constant defined somewhere else */
px = &(table[kx[i]][0][0])+lx;
pz = &(table[kz[i]][0][0])+lz;
^    ^                      ^
|    |                      |
+----+----------------------+---- Therefore none of this makes any sense...

What you're trying to do is have a pointer to some offset in your multidimensional array table, because you can't do that in python, you don't want to "port" this code verbatim.

Follow the logic beyond this, what are you doing with px and pz? That is the code you need to understand to try and port.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no direct equivalent for your C code, since Python has no pointers or pointer arithmetic. Instead, refactor your code to index into the table with bracket notation.

table[kx[i]][0][lx] = 3

would be a rough equivalent of the C

px = &(table[kx[i]][0][0])+lx;
*px = 3;

Note that in Python, your table would not be contiguous. In particular, while this might work in C:

px[10] = 3; // Bounds violation!

This will IndexError in Python:

table[kx[i]][0][lx + 10] = 3

2 Comments

"in Python, your table would not be contiguous" - are you sure about that? I had read "The actual representation of values is determined by the machine architecture (strictly speaking, by the C implementation)." which sure sounds like the arrays would be contiguous in python.
An array would be contiguous, but the table wouldn't be an array. Python doesn't support multidimensional arrays; most code that needs a multidimensional grid of values either uses a list of lists (of lists... for as many levels as needed) or a NumPy ndarray. Some ndarrays are contiguous, but a list of lists definitely isn't. (Also, don't use Python arrays unless you have a very good, specific reason to do so. For most use cases, you want a list.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.