views:

149

answers:

3
self.mood_scale = {
    '-30':"Panic",
    '-20':'Fear',
    '-10':'Concern',
    '0':'Normal',
    '10':'Satisfaction',
    '20':'Happiness',
    '30':'Euphoria'}

I need to set two variables: max_mood and min_mood, so I can put some limits on a ticker. What's the easiest way to get the lowest and the highest keys?

+5  A: 

This should do it:

max_mood = max(self.mood_scale)
min_mood = min(self.mood_scale)

Perhaps not the most efficient (since it has to get and traverse the list of keys twice), but certainly very obvious and clear.

UPDATE: I didn't realize your keys were strings. Since it sounds as if that was a mistake, I'll let this stand as is, but do note that it requires keys to be actual integers.

unwind
The max works fine, but the min gives me -10.
Jorge
@Jorge, because you use strings. And `-10` is lexically before `-20`.
Grzegorz Oledzki
@Jorge: it is using lexicographical ordering of strings. As others have said, you really probably want integer keys. Then you will get the expected order.
PreludeAndFugue
So obvious,innit? I'll go for a boat of caffeine, i think.
Jorge
As the keys are in string format, we should convert it to Integers.Maximum: `max([int(k) for k in self.mood_scale.keys()])`Minimum: `min([int(k) for k in self.mood_scale.keys()])` should work!
abhiomkar
Just so it's clear (see comment on other answer): no need to call the `keys` method here. You can just do `max(self.mood_scale)`.
Thomas Wouters
@Thomas: I removed it. I removed it.
unwind
+7  A: 

Is that valid Python? I think you mean:

mood_scale = {
    '-30':"Panic",
    '-20':'Fear',
    '-10':'Concern',
    '0':'Normal',
    '10':'Satisfaction',
    '20':'Happiness',
    '30':'Euphoria'}

print mood_scale[str(min(map(int,mood_scale)))]
print mood_scale[str(max(map(int,mood_scale)))]

Outputs

Panic Euphoria

Much better and faster with ints as keys

mood_scale = {
    -30:"Panic",
    -20:'Fear',
    -10:'Concern',
    0:'Normal',
    10:'Satisfaction',
    20:'Happiness',
    30:'Euphoria'}

print mood_scale[min(mood_scale))]
print mood_scale[max(mood_scale))]

Edit 2: Is much faster using the iterator

print timeit.timeit( lambda: mood_scale[min(mood_scale.keys())])
print timeit.timeit( lambda: mood_scale[min(mood_scale)])
1.05913901329
0.662925004959

Another solution could be to keep track of the max/min values upon insertion and simply do mood_scale.min() / max()

fabrizioM
Already changed it to ints,and works just fine. Thank you.
Jorge
No need to call `,keys()` in any of the cases; dictionaries are iterables that yield their keys, so `max(mood_scale)` would be fine.
Thomas Wouters
@Thomas-WoutersI like it more in the sense of ' Explicit is Better than implicit. '
fabrizioM
Also in the sense of "slow is better than fast", or "more memory is better than less"? :)
Thomas Wouters
speeeeed wise you are right. Thanks!
fabrizioM
+9  A: 
>>> min(self.mood_scale, key=int)
'-30'
>>> max(self.mood_scale, key=int)
'30'
SilentGhost
+1 for not using `map()`
abhiomkar