<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 28, 2014 at 5:15 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On Fri, Nov 28, 2014 at 3:15 AM, Alexander Belopolsky <<a href="mailto:ndarray@mac.com">ndarray@mac.com</a>> wrote:<br>
> I probably miss something very basic, but how given two arrays a and b, can<br>
> I find positions in a where elements of b are located?  If a were sorted, I<br>
> could use searchsorted, but I don't want to get valid positions for elements<br>
> that are not in a.  In my case, a has unique elements, but in the general<br>
> case I would accept the first match.  In other words, I am looking for an<br>
> array analog of list.index() method.<br>
<br>
</div></div>How about this?<br>
<br>
def index(haystack, needle):<br>
    haystack = np.asarray(haystack)<br>
    haystack_sort = np.argsort(haystack)<br>
    haystack_sorted = haystack[haystack_sort]<br>
    return haystack_sort[np.searchsorted(haystack_sorted, needle)]<br>
<br>
(Note that this will return incorrect results if any entries in needle<br>
are missing from haystack entirely. If this is a concern then you need<br>
to do some extra error-checking on the searchsorted return value.)<br></blockquote><div><br></div><div>I like this approach a lot. You can actually skip the creation of the haystack_sorted array using the sorter kwarg:</div><div><br></div><div>    idx = haystack_sort[np.searchsorted(haystack, needle, sorter=haystack_sort)]</div><div><br></div><div>But either using haystack_sorted or not, if any item in the needle is larger than the largest entry in the haystack, the indexing will error out with an index out of bounds. So the whole thing with proper error checking gets kind of messy, something along the lines of:<br></div><div><br></div><div>    sorted_idx = np.searchsorted(haystack, needle, sorter=haystack_sort)</div><div>    mask_idx = sorted_idx < len(haystack)</div><div>    idx = haystack_sort[sorted_idx[mask_idx]]</div><div>    mask_in_haystack = haystack[idx] == needle[mask_idx]</div><div>    mask_idx[mask_idx] &= mask_in_haystack <br></div><div><br></div><div>So using -1 to indicate items in needle not found in haystack, you could do:<br></div><div><br></div><div>    ret = np.empty_like(needle, dtype=np.intp)</div><div>    ret[~mask_idx] = -1</div><div>    ret[mask_idx] = idx[mask_in_haystack]</div><div><br></div><div>In the end, it does get kind of messy, but I am not sure how could it be improved. Perhaps giving searchsorted an option to figure out the exact matches?<br></div><div><br></div><div>Jaime</div><div><br></div></div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div></div>