Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: cl@isbd.net Newsgroups: comp.lang.python Subject: Re: How to show a dictionary sorted on a value within its data? Date: Wed, 1 Oct 2014 13:16:07 +0100 Lines: 63 Message-ID: <7t7tfb-5lu.ln1@chris.zbmc.eu> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net ompfuyUKV+8OkDjbLQz13AjrgLvnOJyQCzrMliiLt77sQXZkc= X-Orig-Path: not-for-mail Cancel-Lock: sha1:hDNPc3OPuDlU1JKnr5IB2853sIc= User-Agent: tin/2.2.0-20131224 ("Lochindaal") (UNIX) (Linux/3.13.0-36-generic (x86_64)) Xref: csiph.com comp.lang.python:78543 Joel Goldstick wrote: > On Wed, Oct 1, 2014 at 6:45 AM, wrote: > > Peter Otten <__peter__@web.de> wrote: > >> 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 ??? > >> > >> for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain): > >> print(adc.cfg[meas].ain, adc.cfg[meas].Description) > >> > > Brilliant, worked perfectly, thank you. The bit I didn't understamd was > > that 'lambda' bit, but I just looked it up and I'm a bit clearer now. > > > >> or simpler > >> > >> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain")) > >> print(row.ain, row.Description) > >> > > I tried this, I got:- > > > > Traceback (most recent call last): > > File "/home/chris/bin/calibrate.py", line 24, in > > for meas in sorted(adc.cfg.values, > > key=operator.attrgetter("ain")): > > NameError: name 'operator' is not defined > > I must admit that it's the bits like 'operator' in the parameters that > > I can't really understand where they come from. > > > > operator is a module. See > https://docs.python.org/2/library/operator.html#module-operator Ah, OK, as it sort of might be a keyword I hadn't thought of that. :-) -- Chris Green ยท