English 中文(简体)
3. 完善以下法典的写作风格
原标题:better writing style for the following code
  • 时间:2010-11-18 07:11:28
  •  标签:
  • python

我很想知道,有人是否能够告诉我,要检查以下情况,是站不住脚的。

我有6个比值,希望对照其mal价值。 采用数学功能是一种方式,但仍然需要我写大约2**6字,如果构造的话。

因此,我想知道,是否有一份比较容易的文字说明。

并且假设,如果说它不是双向的,那么在平时检查2**6项价值的最佳方式是什么。

if(a==1):
    ....
else:
    if(a==2)
.....

One way is saving it in a list and checking it with the indexes but still that would require that many if-else I guess.....

感谢......。

问题回答

将字典测绘值用于成果(可在沙尔发挥作用)。

例如:

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

outcome = d[a]

自然,这项工作如何依靠你的<条码>......,但这一结构可以非常灵活。 这种办法最重要的特点是,这一字典可以按方案分类,不需要写大量的手工任务。 当然,其效率也大大超过许多代号为>的数值。

我将使用一个矫正器,绘制成基于文字的信号:

_dispatch_table = {}

def dispatch_on(*values):
    def dec(f):
        _dispatch_table.update((v, f) for v in values)
        return f
    return dec

@dispatch_on(0, 2, 47)
def one():
    foo()
    bar()

@dispatch_on(2, 23, 89)
def two():
    bar()
    baz()

x = some_number
_dispatch_table[x]()    

如果我理解<条码>.<>

阁下:

if(a==1):
    ....
else:
    if(a==2)
.....

如果您使用<代码>if elif,则相应:

if a==1:
    ....
elif a==2:
    .....
else:
    default

也可以使用沙尔语为简单的洗衣员作有条件的表述:

def one():
   print("option 1 it is
")

def two():
   print("option 2 it is
")

def three():
   print("not one or two
")

one() if a==1 else two() if a==2 else three()

Or even dictionaries:

def one():
   print("option 1 it is
")

def two():
   print("option 2 it is
")

def three():
   print("not one or two
")

options = {1:one,
           2:two,
           3:three,
}

options[2]()

https://stackoverflow.com/questions/594442/choosing-between-different-switch-case-replacements-in-python-dictionary-or-if>。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签