<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Nov 27, 2014 at 10:15 PM, Alexander Belopolsky <span dir="ltr"><<a href="mailto:ndarray@mac.com" target="_blank">ndarray@mac.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 dir="ltr">I probably miss something very basic, but how given two arrays a and b, can I find positions in a where elements of b are located?  If a were sorted, I could use searchsorted, but I don't want to get valid positions for elements that are not in a.  In my case, a has unique elements, but in the general case I would accept the first match.  In other words, I am looking for an array analog of list.index() method.</div></blockquote><div><br></div><div>I don't know an easy solution to this problem in pure numpy, but if you could do this pretty easily (and quite efficiently) if you are willing to use pandas. Something like:<br></div><div><br></div><div>locs = pd.Index(a).get_indexer(b)</div><div><br></div><div>Note that -1 is used to denote a non-match, and get_indexer will raise if the match is non-unique instead of returning the first element. If your array is not 1d, you can still make this work but you'll need to use np.ravel and np.unravel_index.</div><div><br class="">Actually, you may find that putting your data into pandas data structures is a good solution, since pandas is designed to make exactly these sort of alignment operations easy (and automatic).<br></div><div><br></div><div>I suppose the simplest solution to this problem would be to convert your data into a list and use list.index() repeatedly (or you could even write it yourself in a few lines), but I'd guess that was never implemented for ndarrays because it's rather slow -- better to use a hash-table like a dict or pandas.Index for repeated lookups.<br></div></div></div></div>