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 20 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 1 of 2  [1] 2  Next page →


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

Fromcl@isbd.net
Date2014-10-01 10:58 +0100
SubjectHow to show a dictionary sorted on a value within its data?
Message-ID<crvsfb-drf.ln1@chris.zbmc.eu>
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 ???

-- 
Chris Green
·

[toc] | [next] | [standalone]


#78534

FromPeter Otten <__peter__@web.de>
Date2014-10-01 12:24 +0200
Message-ID<mailman.14512.1412159108.18130.python-list@python.org>
In reply to#78532
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)

or simpler

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

or even

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

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


#78537

Fromcl@isbd.net
Date2014-10-01 11:45 +0100
Message-ID<cj2tfb-80s.ln1@chris.zbmc.eu>
In reply to#78534
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 <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.


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

-- 
Chris Green
·

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


#78538

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-10-01 06:56 -0400
Message-ID<mailman.14515.1412161020.18130.python-list@python.org>
In reply to#78537
On Wed, Oct 1, 2014 at 6:45 AM,  <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.
>
>> 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.
>

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
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

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


#78543

Fromcl@isbd.net
Date2014-10-01 13:16 +0100
Message-ID<7t7tfb-5lu.ln1@chris.zbmc.eu>
In reply to#78538
Joel Goldstick <joel.goldstick@gmail.com> wrote:
> On Wed, Oct 1, 2014 at 6:45 AM,  <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.
> >
> >> 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.
> >
> 
> 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
·

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


#78539

FromPeter Otten <__peter__@web.de>
Date2014-10-01 13:12 +0200
Message-ID<mailman.14516.1412161980.18130.python-list@python.org>
In reply to#78537
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)

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


#78577

FromTravis Griggs <travisgriggs@gmail.com>
Date2014-10-01 21:57 -0700
Message-ID<mailman.14539.1412225868.18130.python-list@python.org>
In reply to#78537

Sent from my iPhone

> On Oct 1, 2014, at 04:12, Peter Otten <__peter__@web.de> wrote:
> 
> `lambda` is just a fancy way to define a function inline

Not sure "fancy" is the correct adjective; more like syntactic tartness (a less sweet version of syntactic sugar).

:)

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


#78592

Fromcl@isbd.net
Date2014-10-02 10:40 +0100
Message-ID<h5jvfb-2cn.ln1@chris.zbmc.eu>
In reply to#78577
Travis Griggs <travisgriggs@gmail.com> wrote:
> 
> 
> Sent from my iPhone
> 
> > On Oct 1, 2014, at 04:12, Peter Otten <__peter__@web.de> wrote:
> > 
> > `lambda` is just a fancy way to define a function inline
> 
> Not sure "fancy" is the correct adjective; more like syntactic tartness 
> (a less sweet version of syntactic sugar). 
> 
It throws me because 'lambda' simply has no meaning whatsoever for me,
i.e. it's just a greek letter.

So from my point of view it's like seeing 'epsilon' stuck in the
middle of some code. 

It's not as if I'm new to programming either, I've been writing
software professionally since the early 1970s, now retired.  I have no
formal computer training, there wasn't much in the way of university
courses on computing in the 1960s, I have a degree in Electrical
Engineering.  Maybe that's why 'lambda' means nothing to me!  :-)

-- 
Chris Green
·

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


#78595

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-10-02 14:17 +0300
Message-ID<878ukyu3e6.fsf@elektro.pacujo.net>
In reply to#78592
cl@isbd.net:

> It's not as if I'm new to programming either, I've been writing
> software professionally since the early 1970s, now retired. I have no
> formal computer training, there wasn't much in the way of university
> courses on computing in the 1960s, I have a degree in Electrical
> Engineering. Maybe that's why 'lambda' means nothing to me! :-)

Lambda is special because it was thought of in the 1920's, well before
there were any general-purpose computers.

<URL: http://www.users.waitrose.com/~hindley/SomePapers_PDFs/2006CarH
in,HistlamRp.pdf>

See Chapter 4.


Marko

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


#78620

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-02 10:46 -0700
Message-ID<35b68df9-3fe2-40f2-aa98-10f4de1ad435@googlegroups.com>
In reply to#78595
On Thursday, October 2, 2014 4:47:50 PM UTC+5:30, Marko Rauhamaa wrote:

> > It's not as if I'm new to programming either, I've been writing
> > software professionally since the early 1970s, now retired. I have no
> > formal computer training, there wasn't much in the way of university
> > courses on computing in the 1960s, I have a degree in Electrical
> > Engineering. Maybe that's why 'lambda' means nothing to me! :-)

> Lambda is special because it was thought of in the 1920's, well before
> there were any general-purpose computers.

> <URL: http://www.users.waitrose.com/~hindley/SomePapers_PDFs/2006CarH

If you find that heavy going, here's an intro lecture on λ-calculus
-- part of lectures on discrete math I've been preparing

https://www.youtube.com/watch?v=FZoq-GlOdSs&feature=youtu.be

Comments/criticisms welcome though do remember I am as new to
film-making today as I was to programming 30 years ago :-)

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


#78605

FromGrant Edwards <invalid@invalid.invalid>
Date2014-10-02 14:05 +0000
Message-ID<m0jm43$pee$1@reader1.panix.com>
In reply to#78592
On 2014-10-02, cl@isbd.net <cl@isbd.net> wrote:
> Travis Griggs <travisgriggs@gmail.com> wrote:
>> 
>> 
>> Sent from my iPhone
>> 
>> > On Oct 1, 2014, at 04:12, Peter Otten <__peter__@web.de> wrote:
>> > 
>> > `lambda` is just a fancy way to define a function inline
>> 
>> Not sure "fancy" is the correct adjective; more like syntactic tartness 
>> (a less sweet version of syntactic sugar). 
>> 
> It throws me because 'lambda' simply has no meaning whatsoever for me,
> i.e. it's just a greek letter.
>
> So from my point of view it's like seeing 'epsilon' stuck in the
> middle of some code. 
>
> It's not as if I'm new to programming either, I've been writing
> software professionally since the early 1970s, now retired.

The use of "lamba" as a keyword to define an anonymous function is
borrowed from Lisp which got it from Lambda calculus.

http://en.wikipedia.org/wiki/Lambda_calculus

-- 
Grant Edwards               grant.b.edwards        Yow! Is this going to
                                  at               involve RAW human ecstasy?
                              gmail.com            

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


#78615

Fromcl@isbd.net
Date2014-10-02 17:33 +0100
Message-ID<gcb0gb-d2u.ln1@chris.zbmc.eu>
In reply to#78605
Grant Edwards <invalid@invalid.invalid> wrote:
> On 2014-10-02, cl@isbd.net <cl@isbd.net> wrote:
> > Travis Griggs <travisgriggs@gmail.com> wrote:
> >> 
> >> 
> >> Sent from my iPhone
> >> 
> >> > On Oct 1, 2014, at 04:12, Peter Otten <__peter__@web.de> wrote:
> >> > 
> >> > `lambda` is just a fancy way to define a function inline
> >> 
> >> Not sure "fancy" is the correct adjective; more like syntactic tartness 
> >> (a less sweet version of syntactic sugar). 
> >> 
> > It throws me because 'lambda' simply has no meaning whatsoever for me,
> > i.e. it's just a greek letter.
> >
> > So from my point of view it's like seeing 'epsilon' stuck in the
> > middle of some code. 
> >
> > It's not as if I'm new to programming either, I've been writing
> > software professionally since the early 1970s, now retired.
> 
> The use of "lamba" as a keyword to define an anonymous function is
> borrowed from Lisp which got it from Lambda calculus.
> 
> http://en.wikipedia.org/wiki/Lambda_calculus
> 
Ah, so at least there is a reason for it, I'm far from being a
mathematician though so it's not particularly obvious (for me anyway).

-- 
Chris Green
·

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


#78622

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-02 13:15 -0600
Message-ID<mailman.14562.1412277409.18130.python-list@python.org>
In reply to#78615
On Thu, Oct 2, 2014 at 10:33 AM,  <cl@isbd.net> wrote:
> Ah, so at least there is a reason for it, I'm far from being a
> mathematician though so it's not particularly obvious (for me anyway).

You're not alone; a lot of people find the terminology not intuitive.
Even GvR has publicly lamented the choice of keyword.

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


#78624

FromGrant Edwards <invalid@invalid.invalid>
Date2014-10-02 19:36 +0000
Message-ID<m0k9g6$5at$1@reader1.panix.com>
In reply to#78615
On 2014-10-02, cl@isbd.net <cl@isbd.net> wrote:
> Grant Edwards <invalid@invalid.invalid> wrote:
>> On 2014-10-02, cl@isbd.net <cl@isbd.net> wrote:
>>
>> > It throws me because 'lambda' simply has no meaning whatsoever for me,
>> > i.e. it's just a greek letter.
>> >
>> > So from my point of view it's like seeing 'epsilon' stuck in the
>> > middle of some code. 
>> >
>> > It's not as if I'm new to programming either, I've been writing
>> > software professionally since the early 1970s, now retired.
>> 
>> The use of "lamba" as a keyword to define an anonymous function is
>> borrowed from Lisp which got it from Lambda calculus.
>> 
>> http://en.wikipedia.org/wiki/Lambda_calculus
>
> Ah, so at least there is a reason for it, I'm far from being a
> mathematician though so it's not particularly obvious (for me
> anyway).

We EE types did get complex number notation using "j" which the
mathematicians find not particularly obvious.

-- 
Grant Edwards               grant.b.edwards        Yow! It's a hole all the
                                  at               way to downtown Burbank!
                              gmail.com            

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


#78626

FromDan Stromberg <drsalists@gmail.com>
Date2014-10-02 13:06 -0700
Message-ID<mailman.14564.1412280412.18130.python-list@python.org>
In reply to#78615
On Thu, Oct 2, 2014 at 12:15 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Oct 2, 2014 at 10:33 AM,  <cl@isbd.net> wrote:
>> Ah, so at least there is a reason for it, I'm far from being a
>> mathematician though so it's not particularly obvious (for me anyway).
>
> You're not alone; a lot of people find the terminology not intuitive.
> Even GvR has publicly lamented the choice of keyword.

Was lambda almost removed from CPython 3.x?

Anyway, pylint doesn't complain about a bare use of lambda, but it
does complain about a map applied to a lambda or a filter applied to a
lambda.  Pylint says they could be replaced by a list comprehension,
with the warning "deprecated-lambda".

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


#78629

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-10-03 09:16 +1000
Message-ID<542ddce8$0$12985$c3e8da3$5496439d@news.astraweb.com>
In reply to#78626
Dan Stromberg wrote:

> On Thu, Oct 2, 2014 at 12:15 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Thu, Oct 2, 2014 at 10:33 AM,  <cl@isbd.net> wrote:
>>> Ah, so at least there is a reason for it, I'm far from being a
>>> mathematician though so it's not particularly obvious (for me anyway).
>>
>> You're not alone; a lot of people find the terminology not intuitive.
>> Even GvR has publicly lamented the choice of keyword.
> 
> Was lambda almost removed from CPython 3.x?

I'm not sure about "almost", but there was serious discussion about removing
it.


> Anyway, pylint doesn't complain about a bare use of lambda, but it
> does complain about a map applied to a lambda or a filter applied to a
> lambda.  Pylint says they could be replaced by a list comprehension,
> with the warning "deprecated-lambda".

The warning name is misleading, lambda is not deprecated. But stylistically,
many people prefer to use a generator expression in place of map or filter:

# instead of this
map(lambda x: 2*x+1, values)

# use this
(2*x+1 for x in values)

# instead of this
filter(lambda x: x > 23, values)

# use this
(x for x in values if x > 23)



-- 
Steven

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


#78633

FromChris Angelico <rosuav@gmail.com>
Date2014-10-03 12:44 +1000
Message-ID<mailman.14568.1412304280.18130.python-list@python.org>
In reply to#78629
On Fri, Oct 3, 2014 at 9:16 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>> Anyway, pylint doesn't complain about a bare use of lambda, but it
>> does complain about a map applied to a lambda or a filter applied to a
>> lambda.  Pylint says they could be replaced by a list comprehension,
>> with the warning "deprecated-lambda".
>
> The warning name is misleading, lambda is not deprecated. But stylistically,
> many people prefer to use a generator expression in place of map or filter:
>
> # instead of this
> map(lambda x: 2*x+1, values)
>
> # use this
> (2*x+1 for x in values)
>
> # instead of this
> filter(lambda x: x > 23, values)
>
> # use this
> (x for x in values if x > 23)

Don't forget that your equivalencies are based on the Py3 map/filter,
which return non-list iterables. There's no really convenient
equivalent to the map() usage of "call this function with each of
these args, and discard the return values", as it looks very odd to do
a list comp for nothing:

[print(x) for x in list_of_strings]

Short of borrowing Pike's automap syntax:

print(list_of_strings[*])

there's really not much that's better than the original map() call. On
the other hand, there aren't actually all that many times when I've
needed to do this, and the simple for loop generally suffices:

for x in list_of_strings: print(x)

So it's not a huge deal.

ChrisA

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


#78642

FromSteven D'Aprano <steve@pearwood.info>
Date2014-10-03 07:55 +0000
Message-ID<542e5677$0$2916$c3e8da3$76491128@news.astraweb.com>
In reply to#78633
On Fri, 03 Oct 2014 12:44:32 +1000, Chris Angelico wrote:

> On Fri, Oct 3, 2014 at 9:16 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>>> Anyway, pylint doesn't complain about a bare use of lambda, but it
>>> does complain about a map applied to a lambda or a filter applied to a
>>> lambda.  Pylint says they could be replaced by a list comprehension,
>>> with the warning "deprecated-lambda".
>>
>> The warning name is misleading, lambda is not deprecated. But
>> stylistically, many people prefer to use a generator expression in
>> place of map or filter:
>>
>> # instead of this
>> map(lambda x: 2*x+1, values)
>>
>> # use this
>> (2*x+1 for x in values)
>>
>> # instead of this
>> filter(lambda x: x > 23, values)
>>
>> # use this
>> (x for x in values if x > 23)
> 
> Don't forget that your equivalencies are based on the Py3 map/filter,

Dan did ask about Python 3.x :-)


> which return non-list iterables. There's no really convenient equivalent
> to the map() usage of "call this function with each of these args, and
> discard the return values", as it looks very odd to do a list comp for
> nothing:

map doesn't discard the return values. It accumulates them in a list, or 
yields them in a list comp.

> [print(x) for x in list_of_strings]

That would be an abuse of a list comprehension, generator expression or 
map since it will produce a whole lot of None values. If you really want 
to discard the return values, put them in a for-loop:

for s in list_of_strings:
    print(s)

> Short of borrowing Pike's automap syntax:
> 
> print(list_of_strings[*])


you mean something like this perhaps?

print(*list_of_strings, sep='\n')


> there's really not much that's better than the original map() call. On
> the other hand, there aren't actually all that many times when I've
> needed to do this, and the simple for loop generally suffices:
> 
> for x in list_of_strings: print(x)

Exactly.


-- 
Steven

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


#78643

FromChris Angelico <rosuav@gmail.com>
Date2014-10-03 18:09 +1000
Message-ID<mailman.14572.1412323799.18130.python-list@python.org>
In reply to#78642
On Fri, Oct 3, 2014 at 5:55 PM, Steven D'Aprano <steve@pearwood.info> wrote:
>> which return non-list iterables. There's no really convenient equivalent
>> to the map() usage of "call this function with each of these args, and
>> discard the return values", as it looks very odd to do a list comp for
>> nothing:
>
> map doesn't discard the return values. It accumulates them in a list, or
> yields them in a list comp.

Sure, but I've seen it used in various places to call functions where
you don't care about the return values. Technically it's identical to
the list comp, but somehow it's seen as less of an abuse.

>> Short of borrowing Pike's automap syntax:
>>
>> print(list_of_strings[*])
>
> you mean something like this perhaps?
>
> print(*list_of_strings, sep='\n')

Well, no. In that one specific example, it happens to be possible to
pass all the args to one print() call and get the same effect, but
imagine some function that doesn't have that particular feature. (I
just used print for simplicity.)

>> there's really not much that's better than the original map() call. On
>> the other hand, there aren't actually all that many times when I've
>> needed to do this, and the simple for loop generally suffices:
>>
>> for x in list_of_strings: print(x)
>
> Exactly.

Yeah. That's the normal way to do things. I'm not entirely sure why I
keep seeing map() for that; there's probably a justification in
functional programming languages, or something.

ChrisA

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


#78627

FromChris Angelico <rosuav@gmail.com>
Date2014-10-03 07:01 +1000
Message-ID<mailman.14565.1412283716.18130.python-list@python.org>
In reply to#78615
On Fri, Oct 3, 2014 at 6:06 AM, Dan Stromberg <drsalists@gmail.com> wrote:
> Anyway, pylint doesn't complain about a bare use of lambda, but it
> does complain about a map applied to a lambda or a filter applied to a
> lambda.  Pylint says they could be replaced by a list comprehension,
> with the warning "deprecated-lambda".

That's not because lambda is a poor choice, but because map() in Py2
code can often be replaced with a list comp. (And map() in Py3 code
can often be replaced by a genexp.)

ChrisA

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web