2020年12月22日星期二

Increment through list and find max value for a range of slices

I have a problem in which I need to find the max values of a range of slices.

Ex: I have a list of 5,000 ints, and want to find the maximum mean for each slice from 1 to 3600 elements.

Currently my code is as follows:

power_vals = # some list / array of ints  for i in range(1, 3600):      max_vals = max([statistics.mean(power_vals[ix:ix+i]) for ix in range(len(power_vals)) if ix+i < len(power_vals)])   

This works fine but it's really slow (for obvious reasons). I tried to use cython to speed up the process. It's obviously better but still not ideal.

Is there a more time efficient way to do this?

https://stackoverflow.com/questions/65417826/increment-through-list-and-find-max-value-for-a-range-of-slices December 23, 2020 at 09:05AM

没有评论:

发表评论