Search code examples
pythonlistmultiplication

Multiplication of lists of different sizes


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

This returns

[1, 3, 5]
[1, 2]
[1, 4]

Now, let's take the first row, [1,3,5] I need to do the following operations pow((sqrt + 1),2) + pow((sqrt + 3), 2) + pow((sqrt + 5),2) and return the sum for all rows separately. So that it returns

515
265
317

I tried adding a nested for loop

for temp in range(n):
    x = templist[temp]
    xsquare = pow(sqrt+x,2) 

but it's not working the way I need it to. Any help will be appreciated, thanks!


Solution

  • Use this function to get the sum:

    def getSum(sublist):
        return sum(pow(sqrt+x, 2) for x in sublist)
    

    Shell example:

    >>> for i in newI:
       valuesofresidues = list(compress(newResidues, i))
       templist = valuesofresidues
       getSum(templist)
    
    
    515.0
    265.0
    317.0