Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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; 'from:addr:yahoo.co.uk': 0.04; 'output': 0.05; 'item.': 0.09; 'lawrence': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'language.': 0.14; 'cmp=': 0.16; 'iirc': 0.16; 'itemgetter': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sorted()': 0.16; 'subject:show': 0.16; 'tuple': 0.16; 'language': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'sort': 0.25; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '???': 0.30; 'quite': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; "can't": 0.35; 'something': 0.35; 'there': 0.35; 'subject:data': 0.36; 'method': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'e.g.': 0.38; 'url:library': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'url:3': 0.61; 'skip:n 10': 0.64; 'our': 0.64 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: How to show a dictionary sorted on a value within its data? Date: Wed, 01 Oct 2014 11:27:04 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-19-87.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412159254 news.xs4all.nl 2849 [2001:888:2000:d::a6]:57277 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78535 On 01/10/2014 10:58, cl@isbd.net wrote: > I have a dictionary as follows:- > > { > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1', conv=6834.374834509803, Description=u'Starter Amps'), > u'LeisureVolts': Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874, Description=u'Leisure Volts'), > u'RudderPos': Row(id=6, ain=u'AIN5', name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'), > u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''), > u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1', conv=32.727273081945, Description=u'Leisure Amps'), > u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts', conv=28.94469628911757, Description=u'Starter Volts') > } > > I want to output a menu to a user comprising some parts of the > dictionary (ain and Description) sorted by ain. > > Is there some incantation of sorted() that will do what I want? I > can't quite fathom out the 'key=' parameter needed to sort it by the > tuple item. Maybe I need a cmp= ? > > E.g. I want to do something like:- > > for meas in sorted(adc.cfg, key=???): > print(adc.cfg[meas].ain, adc.cfg[meas].Description) > > What's needed in the ??? > IIRC one method involves using itemgetter from https://docs.python.org/3/library/operator.html#module-operator -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence