开发者

Randomise order of if-else execution in Python

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 valu开发者_如何学运维es 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.


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.



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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜