2017-07-20 58 views
-4

我怎样才能得到结果和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] 
+0

我们应该知道如何从前两个列表中获得结果吗?或者只是猜测? –

+0

所以基本上,a [0] = 1然后你总结(b [1])或a [1] = 3然后你总结(2,3,4)b,希望它是有道理的。 – Kily

回答

2
a=[1,3,2] 

b= [4,2,3,4,5,10] 

res=[] 

for i in a: 
    res.append(sum(b[:i])) 
    b=b[i:] 

资源存储您的结果

+0

是的,它的确有用。谢谢!! – Kily

-1

这是可能工作正常。

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 
+0

为什么downvote ??? – Maybe

+0

我没有downvote,但使用'list'作为参数是非常糟糕的做法,因为'list'是一个类型名称/内置函数。另外,我不明白这个答案是如何解决问题的,因为你没有解释 –

+0

哦,我明白了,我会解决答案并添加一些解释 – Maybe