把它翻译成python? [英] Translate this to python?

查看:86
本文介绍了把它翻译成python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植从C ++到python的路由。

有一个复杂的for循环,我不知道如何在python中编码


for(i = nPoints-1,j = 0; j< nPoints; i = j,j ++)


谢谢。

解决方案

> for(i = nPoints-1,j = 0; j< nPoints; i = j,j ++)


这个的简单翻译是:

i = npoints-1

$ b $范围内的j(npoints):

......(您的代码在这里)

i = j


HTH,

Pat


> for(i = nPoints-1,j = 0; j< nPoints; i = j,j ++)


或者,如果你不喜欢我和i的初始设置将

更喜欢你在循环顶部的设置代码:


范围内的j(npoints):

i =( j-1)%npoints

...(你的代码在这里)


最后,你总是可以这样做:


pointrange = range(npoints)

for i,j in zip(pointrange [-1:] + pointrange [: - 1],pointrange):

...(你的代码在这里)


如果你在普通的

基础上重复使用这个范围,后者可能会很有用 - - 实际上你可以存储一个旋转点范围。还有一个

点范围。


HTH,

Pat


KraftDiner写道:

我正在移植从C ++到python的路由。
有一个复杂的for循环,我不知道如何编码在python

for(i = nPoints-1,j = 0; j< nPoints; i = j,j ++)




i = nPoints - 1

j = 0

而j < nPoints:

#做东西

i = j

j + = 1


对于C中的循环并不是很复杂。在进入循环之前,它们会发生在
之前发生的事情,在每次传递之前发生在循环顶部的测试

,以及在结束时发生的事情。身体执行后循环只是

。在Python中使用while循环使得

机械翻译任何这样的C for循环...


-Peter


I''m porting a routing from C++ to python.
There is a complex for loop that I don''t know how to code in python

for (i = nPoints-1, j = 0; j < nPoints; i = j, j++)

Thanks.

解决方案

> for (i = nPoints-1, j = 0; j < nPoints; i = j, j++)

A simple translation of this would be:

i = npoints-1

for j in range(npoints):
... (your code here)
i = j

HTH,
Pat


> for (i = nPoints-1, j = 0; j < nPoints; i = j, j++)

Alternatively, if you don''t like the initial setup of i and would
prefer your setup code at the top of the loop:

for j in range(npoints):
i = (j-1) % npoints
... (your code here)

Finally, you could always do something like this:

pointrange = range(npoints)

for i,j in zip(pointrange[-1:]+pointrange[:-1], pointrange):
... (your code here)

This latter could be useful if you are reusing the range on a regular
basis -- in fact you could store a "rotatedpointrange" as well as a
point range.

HTH,
Pat


KraftDiner wrote:

I''m porting a routing from C++ to python.
There is a complex for loop that I don''t know how to code in python

for (i = nPoints-1, j = 0; j < nPoints; i = j, j++)



i = nPoints - 1
j = 0
while j < nPoints:
# do stuff
i = j
j += 1

For loops in C aren''t really complicated. They have stuff that happens
before the loop is entered, a test that occurs at the top of the loop
before each pass, and stuff that happens at the end of the loop just
after the body has executed. Using a while loop in Python makes it
mechanical to translate any such C for loop...

-Peter


这篇关于把它翻译成python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆