简体   繁体   English

如何将这一行 for 循环更改为正常的 for 循环?

[英]How can I change this one line for loop to normal for loop?

My question how can i convert the following one line for loop in this function to normal for loop.我的问题是如何将此 function 中的以下一行 for 循环转换为正常的 for 循环。

def get_batches(dataset, t=1):
   data_order = DataFrame(dataset)  
   h = [data_order.shift(index) for index in range(1, t + 1)]
   h.append(data_order)
   data_order = concat(h, axis=1)                         
   return data_order

When i follow the same, it doesn't work!当我遵循同样的方法时,它不起作用!

So far I've tried this:到目前为止,我已经尝试过:

def get_batches(dataset, t=1):
    data_order = DataFrame(dataset) 
    h=[]
    for index in range(1, t + 1):
         h.append(data_order.shift(index))
    data_order = concat(h, axis=1)
    return data_order

But I wasn't sure if this was right because I was getting an error.但我不确定这是否正确,因为我遇到了错误。

Edit: Another example:编辑:另一个例子:

[variable for variable in listdir(path) if isfile(join(path, variable))]

It is converted as:它被转换为:

variable = []
for i in listdir(path):
     if isfile(join(path, variable)):
         variable.append(i)``` 

You missed the h.append(data_order) from the previous code.您错过了前面代码中的h.append(data_order)

The code should have been like this:代码应该是这样的:

def get_batches(dataset, t=1):
   data_order = DataFrame(dataset)
    h=[]  
   for index in range(1, t + 1)]:
       h.append(data_order.shift(index))
   h.append(data_order)
   data_order = concat(h, axis=1)                         
   return data_order

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

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