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"><<a href="mailto:ggpolo@gmail.com">ggpolo@gmail.com</a>></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 <<a href="mailto:fiorix@gmail.com">fiorix@gmail.com</a>> wrote:<br>
><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<br>
> 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>
> .<br>
<div class="Ih2E3d">> .<br>
><br>
> in order for that sort (and possibly a lot of other things) to work<br>
> 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<br>
> 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',<br>
> 'linda'])]<br>
><br>
><br>
> what do you guys think about it? please correct me if i'm wrong.<br>
><br>
><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>
return len(item[1])<br>
<br>
sorted(friends.items(), key=x)<br>
<br>
Also, your idea ruins the name "itemgetter" 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> - Melville'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