Nice ! Thanks Stéfan. I will add it to the numpy 100 problems.
Nicolas On 07 Aug 2014, at 13:31, Stéfan van der Walt <[email protected]> wrote: > Hi Nicolas > > On Thu, Aug 7, 2014 at 1:16 PM, Nicolas P. Rougier > <[email protected]> wrote: >> Here is a small example: >> >> Z = [(0,0), (1,1), (2,2), (3,3), (4,4)) >> I = [0, 20, 23, 24, 37] >> >> S = [ 20,20,0,24] >> -> Result should be [(1,1), (1,1), (0,0),(3,3)] >> >> S = [15,15] >> -> Wrong (15 not in I) but ideally, I would like this to be converted to >> [(0,0), (0,0)] > > First try: > > Z = np.array([(0,0), (1,1), (2,2), (3,3), (4,4)]) > I = np.array([0, 20, 23, 24, 37]) > S = np.array([ 20,20,0,24,15]) > > out = np.zeros((len(S), len(Z[0]))) > mask = (S[:, np.newaxis] == I) > item, coord = np.where(mask) > out[item, :] = Z[coord] > > Perhaps there's a neater way of doing it! > > Stéfan > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
