<br>hello<br><br>i was thinking about a possible improvement for the itemgetter<br>the documentation page shows simple examples like sorting a dictionary by its integer values, like this:<br><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">inventory</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;apple&#39;</span><span class="p">,</span> <span class="mf">3</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;banana&#39;</span><span class="p">,</span> <span class="mf">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;pear&#39;</span><span class="p">,</span> <span class="mf">5</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;orange&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)]</span><br>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcount</span> <span class="o">=</span> <span class="n">itemgetter</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span><br><span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="n">getcount</span><span class="p">,</span> <span class="n">inventory</span><span class="p">)</span><br>
<span class="go">[3, 2, 5, 1]</span><br><span class="gp">&gt;&gt;&gt; </span><span class="n">sorted</span><span class="p">(</span><span class="n">inventory</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">getcount</span><span class="p">)</span><br>
<span class="go">[(&#39;orange&#39;, 1), (&#39;banana&#39;, 2), (&#39;apple&#39;, 3), (&#39;pear&#39;, 5)]<br></span></pre><br>let&#39;s suppose i have dictionary where items are lists (instead of integers), and i want to sort it by the size of each list:<br>
<br>&gt;&gt;&gt; friends = {<br>...&nbsp; &#39;alex&#39;: [&#39;bob&#39;, &#39;jane&#39;],<br>...&nbsp; &#39;mary&#39;: [&#39;steve&#39;, &#39;linda&#39;, &#39;foo bar&#39;],<br>...&nbsp; &#39;john&#39;: [&#39;max&#39;]<br>... }<br>&gt;&gt;&gt; sorted(friends.items(), key=itemgetter(1))<br>
[(&#39;alex&#39;, [&#39;bob&#39;, &#39;jane&#39;]), (&#39;john&#39;, [&#39;max&#39;]), (&#39;mary&#39;, [&#39;steve&#39;, &#39;linda&#39;, &#39;foo bar&#39;])]<br><br>that doesn&#39;t work since itemgetter(1) will return a list, and that&#39;s not useful for sorting.<br>
<br>i didn&#39;t look at the code, but i suppose itemgetter is something like this:<br><br>class itemgetter:<br>&nbsp;&nbsp;&nbsp; def __init__(self, index):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.index = index<br><br>&nbsp;&nbsp;&nbsp; def __call__(self, item):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return tem[self.index]<br>
<br>in order for that sort (and possibly a lot of other things) to work properly, we could add<br>a callback method for itemgetter, like this:<br><br>class itemgetter:<br>&nbsp;&nbsp;&nbsp; def __init__(self, index, callback=None):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.index = index<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.callback = callback<br><br>&nbsp;&nbsp;&nbsp; def __call__(self, item):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.callback and self.callback(item[self.index]) or item[self.index]<br><br>so, we could easly sort by the amount of data in each list, like this:<br>
<br>&gt;&gt;&gt; sorted(friends.items(), key=itemgetter(1, callback=len))<br>[(&#39;john&#39;, [&#39;max&#39;]), (&#39;alex&#39;, [&#39;bob&#39;, &#39;jane&#39;]), (&#39;foo&#39;, [&#39;bar&#39;, &#39;steve&#39;, &#39;linda&#39;])]<br>
<br><br>what do you guys think about it? please correct me if i&#39;m wrong.<br><br><br>-- <br>Ship ahoy! Hast seen the While Whale?<br> &nbsp;- Melville&#39;s Captain Ahab<br>