python - Numpy using smaller 2D array to map with bigger array with more elements -
i have smaller array as:
a = np.array([2011, 2014, 2015, 2016, 2017]) aval = np.array([14, 10, 35, 40, 45])
i have array:
a2 = np.array([2013, 2014, 2015, 2014, 2015, 2016, 2016, 2016, 2017])
i want create a2val such that:
arval = np.array([10, 35, 10, 35, 40, 40, 40, 45])
so, trying use values in array map elements of a2 , generate extended version of a2val
please note 2011 present in a, 2013 in a2 not in a2 , respectively. can use following suggested in thread:
aval[np.searchsorted(a,a2)]
but doesn't produce answer looking for.
here 1 way:
>>> aval[np.searchsorted(a, a2[np.nonzero(np.in1d(a2, a))[0]])] array([10, 35, 10, 35, 40, 40, 40, 45])
note getting expected indices in default order second array pass searchsorted()
should contain common items first array.
Comments
Post a Comment