Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.04; 'string.': 0.04; 'names.': 0.07; 'python': 0.08; '%s"': 0.09; 'dynamically': 0.09; 'lambda:': 0.09; 'output': 0.10; 'def': 0.13; '"using': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lambda': 0.16; 'message-id:@tim.thechases.com': 0.16; 'mo:': 0.16; 'objects)': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject:sort': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'seems': 0.20; 'cc:no real name:2**0': 0.20; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'skip:m 30': 0.24; 'cc:2**0': 0.24; 'extract': 0.24; "i'm": 0.26; 'import': 0.27; 'partial': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'fairly': 0.30; 'yields': 0.30; 'subject:?': 0.31; 'source': 0.31; 'version': 0.32; 'list': 0.32; 'objects': 0.32; 'sort': 0.33; 'header:User-Agent:1': 0.33; 'function.': 0.34; 'skip:k 20': 0.34; 'unless': 0.35; 'skip:" 10': 0.37; 'but': 0.37; "there's": 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'some': 0.38; 'either': 0.39; 'personal': 0.60; 'subject:any': 0.84; 'bill,': 0.91; 'sort.': 0.91 Date: Tue, 13 Dec 2011 10:08:06 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: tinnews@isbd.co.uk Subject: Re: Using methodcaller in a list sort - any examples anywhere? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323796837 news.xs4all.nl 6871 [2001:888:2000:d::a6]:43281 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17155 On 12/13/11 09:48, tinnews@isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. You want either sorted(..., key=...) to sort and return a copy (leaving the original unmodified) or .sort(key=...) to sort the list in-place: class MyObj(object): def __init__(self, fn): self.fn = fn def get_property(self, s): return "%s: %s" % (s, self.fn) def __str__(self): return self.fn __repr__ = __str__ source = [ MyObj("Doug"), MyObj("Carol"), MyObj("Bill"), MyObj("Adam"), ] print "Unsorted source before:" print repr(source) print "Using a lambda:" print repr(sorted(source, key=lambda mo: mo.get_property("family-name"))) print "Using methodcaller:" from operator import methodcaller print repr(sorted(source, key=methodcaller("get_property", "family-name"))) print "Source still unsorted after:" print repr(source) source.sort(key=lambda mo: mo.get_property("family-name")) print "Source now sorted:" print repr(source) yields the following: Unsorted source before: [Doug, Carol, Bill, Adam] Using a lambda: [Adam, Bill, Carol, Doug] Using methodcaller: [Adam, Bill, Carol, Doug] Source still unsorted after: [Doug, Carol, Bill, Adam] Source now sorted: [Adam, Bill, Carol, Doug] I'm partial to the lambda version over the methodcaller version unless there's a reason to dynamically get the method-name as a string. But that's just a personal preference. -tkc