Creating new list from 2 existing lists, where their values matches
Creating new list from 2 existing lists, where their values matches
I would like to do that in the screenshot. Could you please help me on that?
Code:
import numpy as np
op = np.array([[46, 29],
[39, 47],
[25, 47],
[31, 24]])
rt = np.array([[1, 1, 1, 0], # op[0][1]+op[1][1]+op[2][1]+op[3][0]= 29 + 47 + 47 + 31 = 154
[1, 0, 1, 1],
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 1, 0, 0],
[0, 1, 1, 1],
[0, 1, 0, 1],
[1, 1, 1, 0],
[0, 1, 1, 0],
[1, 0, 1, 0]])

2 Answers
2
This is a pure Python solution. There may be a more performant NumPy solution
[sum(o[r] for o, r in zip(op, x)) for x in rt]
# [154, 139, 146, 142, 132, 164, 142, 154, 171, 146]
What you're doing looks very much like matrix multiplication. Numpy has a matmul operation that will do what you want for you.
np.matmul(rt, op)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code should be in the question so people can copy/paste it into their IDE/terminal. I'd suggest re-writing your question
– AK47
9 mins ago