tags:

views:

111

answers:

4

This might sound like a strange question, but bear with me...

I have a dictionary in Python with values like so:

'B': 23.6
'D': 0.6
'F': 35.9
'H': 35.9

I need to do an if-else with these values to do different things depending which one is > 30. The code I have at the moment is along the lines of:

if angles['B'] > 30:
  # Do stuff
elif angles['D'] > 30:
  # Do other stuff
elif angles['F'] > 30:
  # Do different stuf
elif angles['H'] > 30:
  # Do even more different stuff

Now the problem comes when I have two or more values the same (like in the example data above). In this case I want to randomly pick one of these to use. The question is: how do I do that in Python? Bear in mind that regardless of the values of the dictionary either nothing (if they're all < 30) or only one thing should be done.

+1  A: 

Write a function to scan through the dictionary...

def has_gt_30(d):
    for key, value in d.items():
        if value > 30:
            return key
    return False

angle = has_gt_30(angles_dict)
if angle:
    do_stuff(angle)

This won't pick one randomly, but it will pick one arbitrarily. If you really want a random one, replace the "return key" with an aggregation of keys that fit the criteria and return that. Then use random.choice.

Nathon
+11  A: 

You can make a sequence of key/value pairs:

pairs = angles.iteritems()

Filter it to remove elements <= 30:

filtered = [(name, value) for name, value in pairs if value > 30]

check to see if there are any options

if filtered:

and then pick one:

    from random import choice
    name, value = choice(filtered)

update: added the following...

As Aaron mentions in the comments, this only gets you halfway there. You still need to codify the action you're going to take based on name.

Aaron suggests using a dictionary containing functions. Basically, you would define some functions to do something with your name/value pairs

def thing1(name, value):
    # do stuff...
def thing2(name, value):
    # do different stuff...

set up a dictionary mapping names to function calls

routes = {'A': thing1,
          'B': thing2,
          'C': thing1}

and define a routing function that dispatches to the appropriate function:

def route(pair):
    name, value = pair
    return routes[name](name, value)

Then you can just call route with the name/value pair you get from choice, e.g.

result = route(choice(filtered))

A more structured approach could instead involve creating a class to handle all of this or just the routing aspects.

intuited
beat me too it. I was going to continue on to add that one could use the Dict O' Functions pattern to dispatch on which angle is chosen.
aaronasterling
I DOF my hat to you, sir.
intuited
You don't need the values in the filtered list. Just store the names.
Yuliy
@Yuliy: It depends on what the `# Do stuff` in the original question did; they could reference the values. Actually the route routines might need to have access to the full original dictionary, depending on how the logic works.
intuited
I don't understand why you need to make this so complex, or do I have missed something? (see my answer)
knitti
@knitti: everything from **update** on implements the `# do stuff...` section of your answer. The differences in the stuff above that are mostly a matter of style. I find this way to be a bit more readable, since I don't have to look around to gather from the various parts (`while`, `if`, `break`) what's going on. This way will be a bit less efficient because it's filtering all of the values; that loss is mitigated somewhat because it's not mutating the list with each `pop`. The style used in my answer is I think generally considered to be more idiomatic.
intuited
@knitti: also note that a benefit of the complexity of the "DOF" pattern used here is that it lets you use the same chunk or different chunks of code for different keys. This is basically Python's version of a `case` or `switch` statement.
intuited
I've just deleted my 2 last comments because I realised we won't argue out _here_ our difference what clarity actually _means_ (if that is even possible). Great answer, though.
knitti
+1  A: 

from random import choice

while angles:
    x = choice(angles.keys()) 
    if angles.pop(x)>30:
        if x == 'B':
            # Do stuff
        elif x == 'D':
            # Do other stuff
        elif x == 'F':
           # Do different stuf
        elif x == 'H':
           # Do even more different stuff
        break
knitti
+1  A: 

you can do also:

import random

angles = {'B':23,
'D': 2.6,
'F': 35.9,
'H':35.9
}

keys = angles.keys()

random.shuffle(keys) # shuffle the list of dictionary keys

for key in keys:
   if angles[key] > 30:
      # ...
      break
singularity