[英]Sum of contigous subsequences in an array
Given the following array: 给出以下数组:
tab = [80,12,14,5,70,9,26,30,8,12,16,15]
I want to compute the sum of all possible sequences of size 4 as follow: 我想计算大小为4的所有可能序列的总和如下:
S1=80+12+14+5=111
S2=12+14+5+70 =101
S3=14+5+70+9 =98
....
I have implmented a short program on python to do this and its not efficient: 我已经在python上实现了一个简短的程序来执行此操作并且效率不高:
import numpy as np
tab= np.array([80,12,14,5,70,9,26,30,8,12,16,15])
tab_size=tab.size
n=tab_size
s=0
seq_len=5
for i in range (0,n-(seq_len-1),1):
print("index i ",i)
for k in range(i+1,(seq_len+i),1):
print ("index k ", k)
tab[i]=tab[i]+tab[k]
s=s+1
print(s)
tab
the result is as follow : 结果如下:
array([111, 101, 98, 110, 135, 73, 76, 66, 51, 12, 16, 15])
I notice that each element will participate in the sum operation 4 times which is not good. 我注意到每个元素将参与总和操作4次,这是不好的。 do you have any efficient idea to do that?
你有什么有效的想法吗?
I want to add that, the sequence size is not fixed, in this example is just 4. Thank you in advance 我想补充一点,序列大小不固定,在这个例子中只是4.提前谢谢
S1
, you just need to add 70 and substract 80 to get S2
. S1
,你只需要添加70并减去80来获得S2
。 S2
, you just need to add 9 and substract 12 to get S3
. S2
,你只需要添加9和减去12来获得S3
。 This way, you'll avoid using each element 4 times. 这样,您将避免使用每个元素4次。
Try this, 尝试这个,
print [sum(item) for item in [tab[n:n+4] for n in range(0, len(tab))] if len(item) == 4]
# Result [111, 101, 98, 110, 135, 73, 76, 66, 51]
Short solution using sum
and enumerate
functions: 使用
sum
和enumerate
函数的简短解决方案:
tab = [80,12,14,5,70,9,26,30,8,12,16,15]
sums = [sum(tab[i:i+4]) for i, v in enumerate(tab) if i+4 <= len(tab)]
print(sums)
The output: 输出:
[111, 101, 98, 110, 135, 73, 76, 66, 51]
The last "4-items" consequent sequence to be summed up is 8,12,16,15
(gives 51
) 最后的“4项” 结果序列总结为
8,12,16,15
(给出51
)
I think you can use this approach: 我想你可以使用这种方法:
tab = [80,12,14,5,70,9,26,30,8,12,16,15]
for i in range(len(tab) - 3):
summation = sum(tab[i:i+4])
print(summation)
Another approach would be to keep the cumulitive sum upto current index and subtract the cumulative sum 4 index back, 另一种方法是将累积总和保持在当前指数并减去累积和4指数,
tab = [80,12,14,5,70,9,26,30,8,12,16,15]
cumulative_sum = [0]*(len(tab)+1)
ret = []
for i in xrange(len(tab)):
cumulative_sum[i+1] = cumulative_sum[i] + tab[i]
if i >= 3:
ret.append(cumulative_sum[i+1] - cumulative_sum[i-3])
In place version(without the cumulativeSum
list and storing that in tab
instead), 就地版本(没有
cumulativeSum
列表并将其存储在tab
),
tab = [0] + [80,12,14,5,70,9,26,30,8,12,16,15]
ret = []
for i in xrange(1, len(tab)):
tab[i] += tab[i-1]
if i >= 4:
ret.append(tab[i] - tab[i-4])
It works because it keeps track of the cumulative sum upto every index. 它的工作原理是它跟踪每个索引的累积总和。 So for any index, the sum of the sequence of length
n
ending at that index can be found using cumulativeSum[index] - cumulativeSum[index-n]
因此对于任何索引,可以使用
cumulativeSum[index] - cumulativeSum[index-n]
找到在该索引处结束的长度序列n
的总和
if you want to use numpy 如果你想使用numpy
A=np.array([80, 12, 14, 5, 70, 9, 26, 30, 8, 12, 16, 15])
print reduce(lambda x,y: x + [sum(y)], np.array(zip(A,A[1:],A[2:],A[3:])).tolist(),[])
output 产量
[111, 101, 98, 110, 135, 73, 76, 66, 51]
[111,101,98,110,135,73,76,66,51]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.