On Thu, Nov 27, 2014 at 10:15 PM, Alexander Belopolsky <ndar...@mac.com> wrote:
> 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. > 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: locs = pd.Index(a).get_indexer(b) 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. 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). 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.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion