[Tutor] dictionary values
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Jul 9 02:56:45 CEST 2005
Don Parris said unto the world upon 08/07/2005 20:09:
> On 7/8/05, luke p <rabidpoobear at gmail.com> wrote:
<snip>
>>what I want to do is find out which value in my dictionary is lowest.
>>is there a dictionary function for this, like alpha.min() that will
>>return a key:value pair of the lowest? I cannot find one and I
>>wondered if there was a quick fix to this.
<snip>
>>thanks in advance.
>>-Luke
>
>
>
> I'm new at this, but thought I would throw my $0.02 in the ring for
> the learning experience. I know that sequence operations won't work
> on dictionaries. I wonder if you could get away with a lambda here?
> I'm probably in way over my head, but would something like this work:
>
> min = (lambda x, y: x < y)
> min(1, 9)
>
> Could min() take the dictionary values, as in min(dict[0], dict[9])?
>
> Again, I'm no expert, but having my input picked apart will be a good thing. ;)
>
> Don
Hi Don,
try it out at the prompt :-)
>>> min = (lambda x, y: x < y)
>>> min(1, 9)
True
>>>
So, that's just a complicated way of saying x < y. ;-)
There is also a bit of a consensus (though not unanimous) that using
lambda to get a function bound to a name is to obscure a def. (See
<http://wiki.python.org/moin/DubiousPython#head-f7e19d971af3a70989ba755ff4f5a4194fc4feff>)
Best,
Brian vdB
More information about the Tutor
mailing list