better writing style for the following code

后端 未结 5 644
借酒劲吻你
借酒劲吻你 2021-01-28 09:42

I was wondering if someone could tell me the pythonic way to check out the following.

I have a 6 bit binary number and want to check with its decimal values. Using mathe

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-28 10:07

    Use a dictionary mapping values into outcomes (which can be functions in Python).

    For example:

    d = {}
    d[0] = ....
    d[1] = ....
    d[2] = ....
    
    outcome = d[a]
    

    Naturally, how this works depends on your ...., but this construct can be very flexible. The most important feature of this approach is that this dictionary can be populated programmatically, and you don't need to write a lot of manual assignments. It's of course also much more efficient than going over many values with nested if statements (or elsif)

提交回复
热议问题