python - Sum numpy array values based on labels in a separate array -


i have arrays similar following:

a=[["tennis","tennis","golf","federer","cricket"],    ["federer","nadal","woods","sausage","federer"],    ["sausage","lion","prawn","prawn","sausage"]] 

i have matrix of following weights

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

what looking sum weights based on labels of matrix each row , take top 3 labels row. @ end this:

res=[["cricket","tennis","federer"],      ["federer","sausage","nadal"],      ["lion","sausage","prawn"]] 

in actual data set ties highly unlikely , not concern, cases entire row is:

["federer","federer","federer","federer","federer"] 

ideally returned ["federer","",""].

any guidance appreciated.

see pirsquared answer numpy arrays.

this pure python approach:

for in range(4):     if a[i].count(a[i][0]) == len(a[i]):         res = [a[1][0], "", ""]     else:         res = [x[0] x in sorted(zip(a[i], w[i]), key=lambda c: c[1], reverse=true)[:3]]      print(res) 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -