<div dir="ltr"><div class="markdown-here-wrapper" style="font-size:1em;font-family:Helvetica,arial,freesans,clean,sans-serif;color:rgb(34,34,34);background-color:rgb(255,255,255);border:none;line-height:1.2"><p style="margin:1em 0px">These functions provide a vectorized way of using one array to look up items in another. In particular, they extend the 1d:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;overflow:auto;margin:1em 0px"><code class="hljs language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,255)">a = np.array([<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
b = np.array([<span class="hljs-string">"four"</span>, <span class="hljs-string">"five"</span>, <span class="hljs-string">"six"</span>, <span class="hljs-string">"one"</span>, <span class="hljs-string">"two"</span>, <span class="hljs-string">"three"</span>])
i = a.argsort()
b_sorted = b[i]
</code></pre>
<p style="margin:1em 0px">To work for higher-dimensions:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;overflow:auto;margin:1em 0px"><code class="hljs language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,255)">a = np.array([[<span class="hljs-number">4</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">5</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">6</span>, <span class="hljs-number">3</span>]])
b = np.array([[<span class="hljs-string">"four"</span>, <span class="hljs-string">"one"</span>], [<span class="hljs-string">"five"</span>, <span class="hljs-string">"two"</span>], [<span class="hljs-string">"six"</span>, <span class="hljs-string">"three"</span>]])
i = a.argsort(axis=<span class="hljs-number">1</span>)
b_sorted = np.take_along_axis(b, i, axis=<span class="hljs-number">1</span>)
</code></pre>
<p style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">put_along_axis</code> is the obvious but less useful dual to this operation, inserting elements rather than extracting them. (Unlike <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">put</code> and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">take</code> which are not obvious duals).</p>
<p style="margin:1em 0px">These have been merged in <a href="https://github.com/numpy/numpy/pull/11105" style="color:rgb(51,51,238);text-decoration:none">gh-11105</a>, but as a new addition this probably should have gone by the mailing list first.</p>
<p style="margin:1em 0px">There was a lack of consensus in <a href="https://github.com/numpy/numpy/pull/8714" style="color:rgb(51,51,238);text-decoration:none">gh-8714</a> about how best to generalize to differing dimensions, so only the non-controversial case where the indices and array have the same dimensions was implemented.</p>
<p style="margin:1em 0px">These names were chosen to mirror <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">apply_along_axis</code>, which behaves similarly. Do they seem reasonable?</p><p style="margin:1em 0px">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="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0"></div></div></div>