Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'parameters': 0.04; 'output': 0.05; 'bits': 0.09; 'item.': 0.09; 'parameter': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; '24,': 0.16; 'cmp=': 0.16; 'nameerror:': 0.16; 'sorted()': 0.16; 'subject:show': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'wed,': 0.18; 'bit': 0.19; 'cc:addr:python.org': 0.22; 'simpler': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'skip:" 30': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'chris': 0.29; 'am,': 0.29; '???': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'file': 0.32; 'skip:m 30': 0.32; 'quite': 0.32; 'worked': 0.33; 'url:python': 0.33; '(most': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'module.': 0.36; 'subject:data': 0.36; 'url:listinfo': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'thank': 0.38; 'e.g.': 0.38; 'url:library': 0.38; 'needed': 0.38; 'recent': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'you.': 0.62; 'name': 0.63; 'skip:n 10': 0.64; 'clearer': 0.84; 'otten': 0.84; 'joel': 0.91; 'to:none': 0.92; 'from.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=HKkMaNTYJsWp+aL1+amcvPuaJS3vEBFmaGSSpraaUGo=; b=RqbhHfG/xT7CZ1HL3xVjhHBoEoqCUUbCxAYXVlwfj0m9sMSIiwHNBxicWf6aVDb58K kv7yKmjKiuucsZ4Bs0LMrUpTJ5a449m4tSkvXpO2+gXIwT9yn5Ruark4fcIbgBQTchhx mFrQyvSnFskCGTG7Qg+crTG/+tjuBCsX8hHGNiZWpazIDZ+LQtVr3qNCz6kZHVz+ffWE C4GLR/JCvKvtlkNxQPIbBHZtdn+SZVwfufQElCUgBYMajfn9CfCZJNqvYiU81yqczlVa y8Q4Jgy/oR7HQWt/O2PIT6+wKnvpcLySWowYS7EV6EsmWsnx1nmXcfi7eVmSj4bv++gI 99hQ== MIME-Version: 1.0 X-Received: by 10.224.136.200 with SMTP id s8mr72651032qat.44.1412161017581; Wed, 01 Oct 2014 03:56:57 -0700 (PDT) In-Reply-To: References: Date: Wed, 1 Oct 2014 06:56:57 -0400 Subject: Re: How to show a dictionary sorted on a value within its data? From: Joel Goldstick Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412161020 news.xs4all.nl 2832 [2001:888:2000:d::a6]:39013 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78538 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=3D4, ain=3Du'AIN3', name=3Du'StarterAmps1', >> > conv=3D6834.374834509803, Description=3Du'Starter Amps'), u'LeisureVol= ts': >> > Row(id=3D1, ain=3Du'AIN0', name=3Du'LeisureVolts', conv=3D29.013742159= 95874, >> > Description=3Du'Leisure Volts'), u'RudderPos': Row(id=3D6, ain=3Du'AIN= 5', >> > name=3Du'RudderPos', conv=3D0.028125, Description=3Du'Rudder Position'= ), u'xx': >> > Row(id=3D7, ain=3Du'AIN6', name=3Du'xx', conv=3D0.028125, Description= =3Du''), >> > u'LeisureAmps1': Row(id=3D3, ain=3Du'AIN2', name=3Du'LeisureAmps1', >> > conv=3D32.727273081945, Description=3Du'Leisure Amps'), u'StarterVolts= ': >> > Row(id=3D2, ain=3Du'AIN1', name=3Du'StarterVolts', conv=3D28.944696289= 11757, >> > Description=3Du'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=3D' parameter needed to sort it by the >> > tuple item. Maybe I need a cmp=3D ? >> > >> > E.g. I want to do something like:- >> > >> > for meas in sorted(adc.cfg, key=3D???): >> > print(adc.cfg[meas].ain, adc.cfg[meas].Description) >> > >> > What's needed in the ??? >> >> for meas in sorted(adc.cfg, key=3Dlambda 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=3Doperator.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=3Doperator.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 > >> or even >> >> for row in sorted( >> map(operator.attrgetter("ain", "Description"), adc.cfg.values())= ): >> print(*row) >> > > -- > Chris Green > =C2=B7 > -- > https://mail.python.org/mailman/listinfo/python-list --=20 Joel Goldstick http://joelgoldstick.com