循环列表Python,索引(基本引导程序)

时间:2017-09-27 08:41:00

标签: python list indexing

鉴于以下两个清单:

dates = [1,2,3,4,5]
rates = [0.0154, 0.0169, 0.0179, 0.0187, 0.0194]

我想生成一个列表     df = []

与日期和费率相同的长度(0到4 = 5个元素)在'纯' Python(没有Numpy)作为练习。

df [i]将等于:

df[0] = (1 / (1 + rates[0])  
df[1] = (1 - df[0] * rates[1]) / (1 + rates[1])
...
df[4] = (1 - (df[0] + df[1]..+df[3])*rates[4]) / (1 + rates[4])

我在尝试:

df = [] 
df.append(1 + rates[0])  #create df[0]  
for date in enumerate(dates, start = 1):
   running_sum_vec = 0
   for i in enumerate(rates, start = 1):
       running_sum_vec += df[i] * rates[i]
df[i] = (1 - running_sum_vec) / (1+ rates[i])
return df

但是我得到TypeError:list indices必须是整数。谢谢。

4 个答案:

答案 0 :(得分:1)

因此,enumerate方法返回两个值:index和value

>>> x = ['a', 'b', 'a']
>>> for y_count, y in enumerate(x):
...     print('index: {}, value: {}'.format(y_count, y))
... 
index: 0, value: a
index: 1, value: b
index: 2, value: a

答案 1 :(得分:0)

这是因为for i in enumerate(rates, start = 1):enumerate生成索引的元组和列表中的对象。你应该做点什么

for i, rate in enumerate(rates, start=1):
    running_sum_vec += df[i] * rate

您还需要修复另一个循环(for date in enumerate...)。

您还需要将df[i] = (1 - running_sum_vec) / (1+ rates[i])移回循环(目前它只会设置最后一个值)(并将其更改为追加,因为它目前会尝试设置超出范围的索引)。

答案 2 :(得分:0)

枚举返回索引和条目。 因此,假设列表包含整数,您的代码可以是:

df = [] 
df.append(1 + rates[0])  #create df[0]  
for date in dates:
   running_sum_vec = 0
   for i, rate in enumerate(rates[1:], start = 1):
       running_sum_vec += df[i] * rate
df[i] = (1 - running_sum_vec) / (1+ rate)
return df

虽然我几乎是积极的,但有一种列表理解方式。我将不得不考虑一下。

答案 3 :(得分:0)

不确定这是否是您想要的:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.time.Instant;
import java.util.concurrent.ScheduledFuture;

@Configuration
@ComponentScan
@EnableAutoConfiguration    
public class TaskSchedulingApplication {

    public static void main(String[] args) {
        SpringApplication.run(TaskSchedulingApplication.class, args);
    }

    @Bean
    TaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }
}

@Controller
class ScheduleController {

    public static final long FIXED_RATE = 5000;

    @Autowired
    TaskScheduler taskScheduler;

    ScheduledFuture<?> scheduledFuture;

    @RequestMapping("start")
    ResponseEntity<Void> start() {
        scheduledFuture = taskScheduler.scheduleAtFixedRate(printHour(), FIXED_RATE);

        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    @RequestMapping("stop")
    ResponseEntity<Void> stop() {
        scheduledFuture.cancel(false);
        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    private Runnable printHour() {
        return () -> System.out.println("Hello " + Instant.now().toEpochMilli());
    }

}