If you want to mix data types, you will want structured arrays .
If you need an index of matching values ββin a search array, you want searchsorted
Your example looks like this:
>>> import numpy as np >>> PS = np.array([ ('A', 'LABEL1', 20), ('B', 'LABEL2', 15), ('C', 'LABEL3', 120), ('D', 'LABEL1', 3),], dtype=('a1,a6,i4')) >>> FACTOR = np.array([ ('LABEL1', 0.1), ('LABEL2', 0.5), ('LABEL3', 10)],dtype=('a6,f4'))
Your structured arrays:
>>> PS array([('A', 'LABEL1', 20), ('B', 'LABEL2', 15), ('C', 'LABEL3', 120), ('D', 'LABEL1', 3)], dtype=[('f0', '|S1'), ('f1', '|S6'), ('f2', '<i4')]) >>> FACTOR array([('LABEL1', 0.10000000149011612), ('LABEL2', 0.5), ('LABEL3', 10.0)], dtype=[('f0', '|S6'), ('f1', '<f4')])
And you can access individual fields like this (or you can give them names, see docs):
>>> FACTOR['f0'] array(['LABEL1', 'LABEL2', 'LABEL3'], dtype='|S6')
How to search for a FACTOR on a PS (FACTOR must be sorted):
>>> idx = np.searchsorted(FACTOR['f0'], PS['f1']) >>> idx array([0, 1, 2, 0]) >>> FACTOR['f1'][idx] array([ 0.1, 0.5, 10. , 0.1], dtype=float32)
Now just create a new array and multiply it:
>>> newp = PS.copy() >>> newp['f2'] *= FACTOR['f1'][idx] >>> newp array([('A', 'LABEL1', 2), ('B', 'LABEL2', 7), ('C', 'LABEL3', 1200), ('D', 'LABEL1', 0)], dtype=[('f0', '|S1'), ('f1', '|S6'), ('f2', '<i4')])