Python 2.7 Ineger列表的嵌套列表-执行算术运算

Python 2.7 Ineger列表的嵌套列表-执行算术运算,python-2.7,Python 2.7,我有一个如下的列表,需要先在每个列表中添加项目,然后乘以所有结果2+4=6,3+(-2)=1,2+3+2=7,-7+1=-6,然后6*1*7*(-6)=-252我知道如何通过访问索引来实现,它可以工作(如下),但我也需要以一种无论有多少子列表都可以工作的方式来实现 nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]] a= nested_lst[0][0] + nested_lst[0][1] b= nested_lst[1][0] + nested_lst[

我有一个如下的列表,需要先在每个列表中添加项目,然后乘以所有结果2+4=6,3+(-2)=1,2+3+2=7,-7+1=-6,然后6*1*7*(-6)=-252我知道如何通过访问索引来实现,它可以工作(如下),但我也需要以一种无论有多少子列表都可以工作的方式来实现

nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
a= nested_lst[0][0] + nested_lst[0][1]
b= nested_lst[1][0] + nested_lst[1][1]
c= nested_lst[2][0] + nested_lst[2][1] + nested_lst[2][2]
d= nested_lst[3][0] + nested_lst[3][1]

def sum_then_product(list):
    multip= a*b*c*d
    return multip
print sum_then_product(nested_lst)
我尝试过for循环,它给了我加法,但我不知道如何在这里执行乘法。我是新手。求求你,救命

nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
for i in nested_lst:
    print sum(i)

这就是你要找的吗

nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]] # your list
output = 1 # this will generate your eventual output

for sublist in nested_lst:
    sublst_out = 0
    for x in sublist:
        sublst_out += x # your addition of the sublist elements
    output *= sublst_out # multiply the sublist-addition with the other sublists
print(output)

reduce(operator.mul,map(sum,nested_lst),1)
?非常感谢jonrsharpe-它只比我的高两级:)