I need some help in a little Python problem.
I am trying to write a function that does the following.
The formula works by calculating the DOT product using the list a and table b and then adding the item in list c. So basically first item in list a multiplied with the first sublist item in list b, the the second item in list a multiplied with the second sublist item in list b and then the two multiplications are summed up, which is then added to the first item in list c.
Here is the test case: a = [2.9, 7.5] b = [[4.4, 1.8], [-5.2, 4.2]] c = [-2.8, 3.5]
For example,
(2.9 x 4.4) + (7.5 x 1.8) + (-2.8) = 23.46
The function then does the same for the second item in the lists. The lists a , b and c are not of fixed length and can change according to other inputs of different list lengths. The answer for each loop will be appended into an empty list, which is then returned as the output.
Calling the function will give me an output of:
[23.46, 19.92]
I would like to use for loops to solve this problem as I am not that well-versed at python yet.
Below is my current attempt, it works but it's basically hardcoding and will not accept lists of longer lengths.
def calculate_func(a, b, c): lst = [] for m in range(len(a)): for n in range(len(c)): for i in range(len(b)): for j in range(len(b[0])): output = (a[m-1] * b[i-1][j-1] + a[m] * b[i-1][j]) + c[n-1] output2 = (a[m-1] * b[i][j-1] + a[m] * b[i][j]) + c[n] lst = [output, output2] return lst
Thank you for helping!
https://stackoverflow.com/questions/67030690/python-multiplying-list-with-a-list-with-sublists April 10, 2021 at 12:04PM
没有评论:
发表评论