On Mi, 2015-12-30 at 20:21 +0100, Nicolas P. Rougier wrote: > In the end, I’ve only the list comprehension to work as expected > > A = [0,0,1,3] > B = np.arange(8) > np.random.shuffle(B) > I = [list(B).index(item) for item in A if item in B] > > > But Mark's and Sebastian's methods do not seem to work... >
Yeah, sorry had a mind slip with the sorter since it returns the sorted version. I think this should do the correct thing (throws away invalid ones as default, though I think it is a bad idea in general). def index(A, B, fill_invalid=None): B_sorter = np.argsort(B) B_sorted = B[B_sorter] B_sorted_index = np.searchsorted(B_sorted, A) # Go back into the original index: B_index = B_sorter[B_sorted_index] if fill_invalid is None: valid = B.take(B_index, mode='clip') == A return B_index[valid] else: invalid = B.take(B_index, mode='clip') != A B_index[invalid] = fill_invalid return B_index > > > > On 30 Dec 2015, at 19:51, Nicolas P. Rougier < > > nicolas.roug...@inria.fr> wrote: > > > > > > Unfortunately, this does not handle repeated entries in a. > > > > > On 30 Dec 2015, at 19:40, Mark Miller <markperrymil...@gmail.com> > > > wrote: > > > > > > I was not familiar with the .in1d function. That's pretty handy. > > > > > > Yes...it looks like numpy.where(numpy.in1d(b, a)) does what you > > > need. > > > > > > > > > numpy.where(numpy.in1d(b, a)) > > > (array([1, 2, 5, 7], dtype=int64),) > > > It would be interesting to see the benchmarks. > > > > > > > > > On Wed, Dec 30, 2015 at 10:17 AM, Nicolas P. Rougier < > > > nicolas.roug...@inria.fr> wrote: > > > > > > Yes, it is the expected result. Thanks. > > > Maybe the set(a) & set(b) can be replaced by > > > np.where[np.in1d(a,b)], no ? > > > > > > > On 30 Dec 2015, at 18:42, Mark Miller < > > > > markperrymil...@gmail.com> wrote: > > > > > > > > I'm not 100% sure that I get the question, but does this help > > > > at all? > > > > > > > > > > > a = numpy.array([3,2,8,7]) > > > > > > > b = numpy.array([1,3,2,4,5,7,6,8,9]) > > > > > > > c = set(a) & set(b) > > > > > > > c #contains elements of a that are in b (and vice versa) > > > > set([8, 2, 3, 7]) > > > > > > > indices = numpy.where([x in c for x in b])[0] > > > > > > > indices #indices of b where the elements of a in b occur > > > > array([1, 2, 5, 7], dtype=int64) > > > > > > > > -Mark > > > > > > > > > > > > On Wed, Dec 30, 2015 at 6:45 AM, Nicolas P. Rougier < > > > > nicolas.roug...@inria.fr> wrote: > > > > > > > > I’m scratching my head around a small problem but I can’t find > > > > a vectorized solution. > > > > I have 2 arrays A and B and I would like to get the indices > > > > (relative to B) of elements of A that are in B: > > > > > > > > > > > A = np.array([2,0,1,4]) > > > > > > > B = np.array([1,2,0]) > > > > > > > print (some_function(A,B)) > > > > [1,2,0] > > > > > > > > # A[0] == 2 is in B and 2 == B[1] -> 1 > > > > # A[1] == 0 is in B and 0 == B[2] -> 2 > > > > # A[2] == 1 is in B and 1 == B[0] -> 0 > > > > > > > > Any idea ? I tried numpy.in1d with no luck. > > > > > > > > > > > > Nicolas > > > > > > > > _______________________________________________ > > > > NumPy-Discussion mailing list > > > > NumPy-Discussion@scipy.org > > > > https://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > > > > > _______________________________________________ > > > > NumPy-Discussion mailing list > > > > NumPy-Discussion@scipy.org > > > > https://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > > > _______________________________________________ > > > NumPy-Discussion mailing list > > > NumPy-Discussion@scipy.org > > > https://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > > > _______________________________________________ > > > NumPy-Discussion mailing list > > > NumPy-Discussion@scipy.org > > > https://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > _______________________________________________ > > NumPy-Discussion mailing list > > NumPy-Discussion@scipy.org > > https://mail.scipy.org/mailman/listinfo/numpy-discussion > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > https://mail.scipy.org/mailman/listinfo/numpy-discussion
signature.asc
Description: This is a digitally signed message part
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion