简体   繁体   English

R 代码片段到 Python

[英]R snippet to Python

I would appreciate if someone could provide support in translating this for loop written in R to Python:如果有人可以提供支持将用 R 编写的 for 循环翻译成 Python,我将不胜感激:

iGs<-c()
for (B in 602:6000)
{
  iG3=(2*iG2)-iG1
  iGs[B-602+1]<-c(iG3)
  iG1=iG2
  iG2=iG3
}

assuming that iG1=45 and iG2=46 (ie. single values).假设iG1=45iG2=46 (即单一值)。 As I understand it (newbie in Python here), it should be something like this:据我了解(这里是 Python 新手),它应该是这样的:

B = range(602,6001,1)
iG3 = (2*iG2)-iG1
iGs = [???]

where iGs is the final list of values (?) after iterating from 602 to 6000, and also after concatenating those values corresponding to 600 and 601 that were previously calculated (ie. iG1 and iG2 ).其中iGs是从 602 迭代到 6000 之后的最终值列表 (?),也是在连接与先前计算的 600 和 601 对应的那些值(即iG1iG2 )之后。

Any help is much appreciated.任何帮助深表感谢。

We could initialize a list in Python我们可以在Python初始化一个list

iGs = []

and appendappend

for b in range(602,6001,1):
    iG3 = (2*iG2)-iG1
    iGs.append(iG3)
    iG1=iG2
    iG2=iG3

-checking the output - 检查输出

len(iGs)
#5399

iGs[-5:len(iGs)]
#[5441, 5442, 5443, 5444, 5445]
iGs[0:5]
#[47, 48, 49, 50, 51]

Comparing with RR比较

length(iGs)
#[1] 5399

head(iGs)
#[1] 47 48 49 50 51 52

tail(iGs)
#[1] 5440 5441 5442 5443 5444 5445  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM