Python 基于索引列表对列表元素求和

Python 基于索引列表对列表元素求和,python,list,Python,List,我怎样才能得到结果和结果?请引导我通过,我真的很感激 x=[1,2] y=[3,4,5] result= [3,9] <=== the sum of result determine by x res存储您的结果它可能工作正常 def isValid(counter, array): # valid if sum of counter equals array length return sum(counter) == len(array) def cal

我怎样才能得到结果和结果?请引导我通过,我真的很感激

x=[1,2]      

y=[3,4,5]

result= [3,9]   <===  the sum of result determine by x
res存储您的结果

它可能工作正常

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,希望这是有意义的。我没有投反对票,但是使用
list
作为参数是非常糟糕的做法,因为
list
是一个类型名/内置函数。另外,我不明白这个答案是如何解决问题的,因为你们并没有解释。哦,我明白了,我会修正这个答案并添加一些解释
a=[1,3,2]

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

res=[]

for i in a:
    res.append(sum(b[:i]))
    b=b[i:]
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