我怎样才能得到结果和abResult?请引导我通过,我真的很感激它!基于索引列表求和列表元素
x=[1,2]
y=[3,4,5]
result= [3,9] <=== the sum of result determine by x
============= OR ==========
a=[1,3,2]
b= [4,2,3,4,5,10]
abResult= [4,9,15]
我怎样才能得到结果和abResult?请引导我通过,我真的很感激它!基于索引列表求和列表元素
x=[1,2]
y=[3,4,5]
result= [3,9] <=== the sum of result determine by x
============= OR ==========
a=[1,3,2]
b= [4,2,3,4,5,10]
abResult= [4,9,15]
a=[1,3,2]
b= [4,2,3,4,5,10]
res=[]
for i in a:
res.append(sum(b[:i]))
b=b[i:]
资源存储您的结果
是的,它的确有用。谢谢!! – Kily
这是可能工作正常。
def isValid(counter, array): # valid if sum of counter equals array length
return sum(counter) == len(array)
def calc(counter, array):
if not isValid(counter, array):
return []
flag = 0
for x in counter:
yield sum(array[flag:flag+x]) # sum between (flag ~ flag+x)
flag += x # add x to flag for next flag
我们应该知道如何从前两个列表中获得结果吗?或者只是猜测? –
所以基本上,a [0] = 1然后你总结(b [1])或a [1] = 3然后你总结(2,3,4)b,希望它是有道理的。 – Kily