Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17155

Re: Using methodcaller in a list sort - any examples anywhere?

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
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 <hm8kr8-uge.ln1@chris.zbmc.eu>
In-Reply-To <hm8kr8-uge.ln1@chris.zbmc.eu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3607.1323796837.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Using methodcaller in a list sort - any examples anywhere? tinnews@isbd.co.uk - 2011-12-13 15:48 +0000
  Re: Using methodcaller in a list sort - any examples anywhere? Peter Otten <__peter__@web.de> - 2011-12-13 17:13 +0100
    Re: Using methodcaller in a list sort - any examples anywhere? tinnews@isbd.co.uk - 2011-12-13 16:48 +0000
  Re: Using methodcaller in a list sort - any examples anywhere? Tim Chase <python.list@tim.thechases.com> - 2011-12-13 10:08 -0600

csiph-web