<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">>>> </span><span class="n">inventory</span> <span class="o">=</span> <span class="p">[(</span><span class="s">'apple'</span><span class="p">,</span> <span class="mf">3</span><span class="p">),</span> <span class="p">(</span><span class="s">'banana'</span><span class="p">,</span> <span class="mf">2</span><span class="p">),</span> <span class="p">(</span><span class="s">'pear'</span><span class="p">,</span> <span class="mf">5</span><span class="p">),</span> <span class="p">(</span><span class="s">'orange'</span><span class="p">,</span> <span class="mf">1</span><span class="p">)]</span><br>
<span class="gp">>>> </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">>>> </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">>>> </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">[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]<br></span></pre><br>let'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>>>> friends = {<br>... 'alex': ['bob', 'jane'],<br>... 'mary': ['steve', 'linda', 'foo bar'],<br>... 'john': ['max']<br>... }<br>>>> sorted(friends.items(), key=itemgetter(1))<br>
[('alex', ['bob', 'jane']), ('john', ['max']), ('mary', ['steve', 'linda', 'foo bar'])]<br><br>that doesn't work since itemgetter(1) will return a list, and that's not useful for sorting.<br>
<br>i didn't look at the code, but i suppose itemgetter is something like this:<br><br>class itemgetter:<br> def __init__(self, index):<br> self.index = index<br><br> def __call__(self, item):<br> 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> def __init__(self, index, callback=None):<br> self.index = index<br>
self.callback = callback<br><br> def __call__(self, item):<br> 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>>>> sorted(friends.items(), key=itemgetter(1, callback=len))<br>[('john', ['max']), ('alex', ['bob', 'jane']), ('foo', ['bar', 'steve', 'linda'])]<br>
<br><br>what do you guys think about it? please correct me if i'm wrong.<br><br><br>-- <br>Ship ahoy! Hast seen the While Whale?<br> - Melville's Captain Ahab<br>