import numpy as np
newResidues = [1, 2, 3, 4, 5]
newI = [[1,0,1,0,1],[1,1,0,0,0],[1,0,0,1,0]]
sqrt = 10
templist = []
from itertools import compress
for i in newI:
valuesofresidues = list(compress(newResidues, i))
templist = valuesofresidues
print templist
返回
[1, 3, 5]
[1, 2]
[1, 4]
现在,让我们走第一行,[1,3,5]
我需要做以下操作
pow((sqrt + 1),2) + pow((sqrt + 3), 2) + pow((sqrt + 5),2)
并分别返回所有行的总和。所以它返回
515
265
317
我尝试添加嵌套for循环
for temp in range(n):
x = templist[temp]
xsquare = pow(sqrt+x,2)
但它不按照我需要的方式工作。 任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
使用此功能获得总和:
def getSum(sublist):
return sum(pow(sqrt+x, 2) for x in sublist)
Shell示例:
>>> for i in newI:
valuesofresidues = list(compress(newResidues, i))
templist = valuesofresidues
getSum(templist)
515.0
265.0
317.0