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


Groups > comp.lang.python > #78539

Re: How to show a dictionary sorted on a value within its data?

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'parameters': 0.04; 'output': 0.05; 'bits': 0.09; 'item.': 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; 'def': 0.12; '24,': 0.16; 'cmp=': 0.16; 'fancy': 0.16; 'lambda': 0.16; 'nameerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorted()': 0.16; 'subject:show': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'bit': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'simpler': 0.24; 'sort': 0.25; 'define': 0.26; 'skip:" 30': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; '???': 0.30; "i'm": 0.30; 'file': 0.32; 'skip:m 30': 0.32; 'quite': 0.32; 'worked': 0.33; '(most': 0.33; 'beginning': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'module.': 0.36; 'subject:data': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'thank': 0.38; 'e.g.': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'you.': 0.62; "you've": 0.63; 'name': 0.63; 'skip:n 10': 0.64; 'clearer': 0.84; 'otten': 0.84; 'joel': 0.91; 'from.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to show a dictionary sorted on a value within its data?
Date Wed, 01 Oct 2014 13:12:37 +0200
Organization None
References <crvsfb-drf.ln1@chris.zbmc.eu> <mailman.14512.1412159108.18130.python-list@python.org> <cj2tfb-80s.ln1@chris.zbmc.eu>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8fb6.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14516.1412161980.18130.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412161980 news.xs4all.nl 2856 [2001:888:2000:d::a6]:41883
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:78539

Show key headers only | View raw


cl@isbd.net 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.

`lambda` is just a fancy way to define a function inline. With a normal 
`def` it would be

def get_ain(key):
    return adc.cfg[key].ain

or meas in sorted(adc.cfg, key=get_ain):
   print(adc.cfg[meas].ain, adc.cfg[meas].Description)

> 
>> 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 <module>
>           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.

As Joel says, operator is a module. To use it add

import operator

at the beginning of your module. To check if you've really understood the 
lambda you can try to replace the operator.attrgetter("ain") in

>> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain")):

with 

for row in sorted(adc.cfg.values(), key=lambda ...):

>> or even
>> 
>> for row in sorted(
>>         map(operator.attrgetter("ain", "Description"),
>>         adc.cfg.values())):
>>     print(*row)

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


Thread

How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-01 10:58 +0100
  Re: How to show a dictionary sorted on a value within its data? Peter Otten <__peter__@web.de> - 2014-10-01 12:24 +0200
    Re: How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-01 11:45 +0100
      Re: How to show a dictionary sorted on a value within its data? Joel Goldstick <joel.goldstick@gmail.com> - 2014-10-01 06:56 -0400
        Re: How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-01 13:16 +0100
      Re: How to show a dictionary sorted on a value within its data? Peter Otten <__peter__@web.de> - 2014-10-01 13:12 +0200
      Re: How to show a dictionary sorted on a value within its data? Travis Griggs <travisgriggs@gmail.com> - 2014-10-01 21:57 -0700
        Re: How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-02 10:40 +0100
          Re: How to show a dictionary sorted on a value within its data? Marko Rauhamaa <marko@pacujo.net> - 2014-10-02 14:17 +0300
            Re: How to show a dictionary sorted on a value within its data? Rustom Mody <rustompmody@gmail.com> - 2014-10-02 10:46 -0700
          Re: How to show a dictionary sorted on a value within its data? Grant Edwards <invalid@invalid.invalid> - 2014-10-02 14:05 +0000
            Re: How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-02 17:33 +0100
              Re: How to show a dictionary sorted on a value within its data? Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-02 13:15 -0600
              Re: How to show a dictionary sorted on a value within its data? Grant Edwards <invalid@invalid.invalid> - 2014-10-02 19:36 +0000
              Re: How to show a dictionary sorted on a value within its data? Dan Stromberg <drsalists@gmail.com> - 2014-10-02 13:06 -0700
                Re: How to show a dictionary sorted on a value within its data? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-03 09:16 +1000
                Re: How to show a dictionary sorted on a value within its data? Chris Angelico <rosuav@gmail.com> - 2014-10-03 12:44 +1000
                Re: How to show a dictionary sorted on a value within its data? Steven D'Aprano <steve@pearwood.info> - 2014-10-03 07:55 +0000
                Re: How to show a dictionary sorted on a value within its data? Chris Angelico <rosuav@gmail.com> - 2014-10-03 18:09 +1000
              Re: How to show a dictionary sorted on a value within its data? Chris Angelico <rosuav@gmail.com> - 2014-10-03 07:01 +1000
  Re: How to show a dictionary sorted on a value within its data? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-01 11:27 +0100
  Re: How to show a dictionary sorted on a value within its data? Joel Goldstick <joel.goldstick@gmail.com> - 2014-10-01 06:37 -0400
    Re: How to show a dictionary sorted on a value within its data? cl@isbd.net - 2014-10-01 13:19 +0100

csiph-web