gpt4 book ai didi

python - Python 中的等效表达式

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:12 25 4
gpt4 key购买 nike

我是 Python n00b,冒着问基本问题的风险,我开始了。

出于各种我不想讨论的原因,我正在将一些代码从 C 移植到 Python。

在 C 代码中,我在下面复制了一些代码。

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

我想要下面 C 代码的等效 Python 表达式:

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;

谁能帮我用 Python 给出等效的表达式?

最佳答案

事情是这样的……你不能在 python 中做指针,所以你在这里展示的东西在某种意义上不是“可移植的”:

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...

你想要做的是在你的多维数组 table 中有一个指向某个偏移量的指针,因为你不能在 python 中这样做,你不想“移植”这个逐字编码。

按照除此之外的逻辑,你用 pxpz 做什么?这是您尝试和移植需要理解的代码。

关于python - Python 中的等效表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17727580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com