A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20090111/de746119/attachment.htm below:

<br>thanks!<br><br><div class="gmail_quote">On Sun, Jan 11, 2009 at 2:12 PM, Guilherme Polo <span dir="ltr">&lt;<a href="mailto:ggpolo@gmail.com">ggpolo@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Sun, Jan 11, 2009 at 2:02 PM, Alexandre Fiori &lt;<a href="mailto:fiorix@gmail.com">fiorix@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; hello<br>
&gt;<br>
&gt; i was thinking about a possible improvement for the itemgetter<br>
&gt; the documentation page shows simple examples like sorting a dictionary by<br>
&gt; its integer values<br>
<br>
</div>Hi,<br>
<br>
Sorry for starting like this but ideas are supposed to be emailed to<br>
the python-ideas maillist.<br>
<br>
&gt; .<br>
<div class="Ih2E3d">&gt; .<br>
&gt;<br>
&gt; in order for that sort (and possibly a lot of other things) to work<br>
&gt; properly, we could add<br>
&gt; a callback method for itemgetter, like this:<br>
&gt;<br>
&gt; class itemgetter:<br>
&gt; &nbsp; &nbsp; def __init__(self, index, callback=None):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.index = index<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.callback = callback<br>
&gt;<br>
&gt; &nbsp; &nbsp; def __call__(self, item):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; return self.callback and self.callback(item[self.index]) or<br>
&gt; item[self.index]<br>
&gt;<br>
&gt; so, we could easly sort by the amount of data in each list, like this:<br>
&gt;<br>
&gt;&gt;&gt;&gt; sorted(friends.items(), key=itemgetter(1, callback=len))<br>
&gt; [(&#39;john&#39;, [&#39;max&#39;]), (&#39;alex&#39;, [&#39;bob&#39;, &#39;jane&#39;]), (&#39;foo&#39;, [&#39;bar&#39;, &#39;steve&#39;,<br>
&gt; &#39;linda&#39;])]<br>
&gt;<br>
&gt;<br>
&gt; what do you guys think about it? please correct me if i&#39;m wrong.<br>
&gt;<br>
&gt;<br>
<br>
</div>You are not forced to use itemgetter as a key in sorted, you can<br>
provide your own key method, like this:<br>
<br>
def x(item):<br>
 &nbsp; &nbsp;return len(item[1])<br>
<br>
sorted(friends.items(), key=x)<br>
<br>
Also, your idea ruins the name &quot;itemgetter&quot; since it is no longer a itemgetter.<br>
<font color="#888888"><br>
--<br>
-- Guilherme H. Polo Goncalves<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Ship ahoy! Hast seen the While Whale?<br> &nbsp;- Melville&#39;s Captain Ahab<br>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4