简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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