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


Groups > comp.lang.python > #78532 > unrolled thread

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

Started bycl@isbd.net
First post2014-10-01 10:58 +0100
Last post2014-10-01 13:19 +0100
Articles 3 on this page of 23 — 13 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#78535

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-01 11:27 +0100
Message-ID<mailman.14513.1412159254.18130.python-list@python.org>
In reply to#78532
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

[toc] | [prev] | [next] | [standalone]


#78536

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-10-01 06:37 -0400
Message-ID<mailman.14514.1412159881.18130.python-list@python.org>
In reply to#78532
On Wed, Oct 1, 2014 at 5:58 AM,  <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')
> }

Is Row a function?  Or do you have a key with a tuple as the value?
>
> 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 ???
>
> --
> Chris Green
> ·

You might read here:
http://www.pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#78546

Fromcl@isbd.net
Date2014-10-01 13:19 +0100
Message-ID<748tfb-5lu.ln1@chris.zbmc.eu>
In reply to#78536
Joel Goldstick <joel.goldstick@gmail.com> wrote:
> On Wed, Oct 1, 2014 at 5:58 AM,  <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') 
> > }
> 
> Is Row a function?  Or do you have a key with a tuple as the value?

Row is a namedtuple, the dictionary is extracted from a database with
a row factory so the namedtuple names are the column names.  It means
that if I change a column name then most things continue to work
without any code changes, and also I can refer to items in the tuple
by name of course.

I have the answer to my question in other responses.

-- 
Chris Green
·

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web