如何将这段代码优化为几行? (Python 3.X)

时间:2015-11-22 13:53:04

标签: python loops python-3.x optimization

我坚持使用这段代码,必须进行优化。事实是,我无法让它发挥作用。

if condition1 == 0:
    A_value1 = (1/6)
    A_value2 = (1/8)
if condition1 == 1:
    A_value1 = (1/2)
    A_value2 = (1/8)
if condition1 == 2:
    A_value1 = (5/6)
    A_value2 = (1/8)
if condition1 == 3:
    A_value1 = (1/6)
    A_value2 = (3/8)
if condition1 == 4:
    A_value1 = (1/2)
    A_value2 = (3/8)
if condition1 == 5:
    A_value1 = (5/6)
    A_value2 = (3/8)
if condition1 == 6:
    A_value1 = (1/6)
    A_value2 = (5/8)
if condition1 == 7:
    A_value1 = (1/2)
    A_value2 = (5/8)
if condition1 == 8:
    A_value1 = (5/6)
    A_value2 = (5/8)

if condition2 == 0:
    B_value1 = (1/6)
    B_value2 = (1/8)
if condition2 == 1:
    B_value1 = (1/2)
    B_value2 = (1/8)
if condition2 == 2:
    B_value1 = (5/6)
    B_value2 = (1/8)
if condition2 == 3:
    B_value1 = (1/6)
    B_value2 = (3/8)
if condition2 == 4:
    B_value1 = (1/2)
    B_value2 = (3/8)
if condition2 == 5:
    B_value1 = (5/6)
    B_value2 = (3/8)
if condition2 == 6:
    B_value1 = (1/6)
    B_value2 = (5/8)
if condition2 == 7:
    B_value1 = (1/2)
    B_value2 = (5/8)
if condition2 == 8:
    B_value1 = (5/6)
    B_value2 = (5/8)

if condition3 == 0:
    C_value1 = (1/6)
    C_value2 = (1/8)
if condition3 == 1:
    C_value1 = (1/2)
    C_value2 = (1/8)
if condition3 == 2:
    C_value1 = (5/6)
    C_value2 = (1/8)
if condition3 == 3:
    C_value1 = (1/6)
    C_value2 = (3/8)
if condition3 == 4:
    C_value1 = (1/2)
    C_value2 = (3/8)
if condition3 == 5:
    C_value1 = (5/6)
    C_value2 = (3/8)
if condition3 == 6:
    C_value1 = (1/6)
    C_value2 = (5/8)
if condition3 == 7:
    C_value1 = (1/2)
    C_value2 = (5/8)
if condition3 == 8:
    C_value1 = (5/6)
    C_value2 = (5/8)

A_value1,B_value1和C_value1在1 / 6,3 / 6和5/6之间交替。

A_value2,B_value2和C_value2在1 / 8,3 / 8和5/8之间交替。

我尝试过循环,范围等,但我的努力是徒劳的。它可以在单个循环/范围(?) - 段中完成,还是必须分为condition1,condition2和condition3。有什么想法吗?

4 个答案:

答案 0 :(得分:1)

这些条件看起来像任意菜单选项,这是众所周知难以整理的。但是,有一点你可以做。由于条件在不同条件下是一致的,因此您可以将它们存储在字典中并根据需要进行查找。将条件存储在list中,然后构建list个值。

c = [condition1, condition2, condition3]
vals = []
lookup = {0:(1/6, 1/8), 1:(1/2, 1/8), 2:(5/6, 1/8), 3:(1/6, 3/8), 4:(1/2, 3/8),
          5:(5/6, 3/8), 6:(1/6, 5/8), 7:(1/2, 5/8), 8:(5/6, 5/8)

for i in range(3):
    vals.append(lookup[condition[i]])

然后,您会将A_value1称为vals[0][0],将C_value2称为vals[2][1],依此类推。

答案 1 :(得分:1)

您可以引入可能值的列表,并根据条件除以3的余数从值列表中获取A_value1等,并根据整数除法的结果得到A_value2条件由3:

values1 = [1/6, 3/6, 5/6]
values2 = [1/8, 3/8, 5/8]
A_value1 = values1[condition1 % 3]
A_value2 = values2[condition1 // 3]
B_value1 = values1[condition2 % 3]
B_value2 = values2[condition2 // 3]
C_value1 = values1[condition3 % 3]
C_value2 = values2[condition3 // 3]

答案 2 :(得分:0)

哇。是的,太可怕了。对于一个简单的解决方案,您可以使用分配创建硬编码数组。它们在每个部分看起来都是一样的。

values = [[(1/6), (1/8)], [(1/2), (1/8)], [(5/6), (5/8)] ....]

然后c_value1将为values[condition3][0]c_value2将为values[condition3][1]等。

您可以使用array攻击condition valueindex

只是一个想法。

答案 3 :(得分:0)

这里有数学关系:

def f(condition):
    return ((condition % 3)*2 + 1)/6, ((condition // 3)*2 + 1)/8

A_value1, A_value2 = f(condition1)
B_value1, B_value2 = f(condition2)
C_value1, C_value2 = f(condition3)

我还会找到一种方法,不在我的变量中包含数据(例如condition1condition2可能会更好地作为单个conditions列表。

在代码中,这将允许我们执行以下操作:

# we wouldn't need this if we already avoided building condition1
conditions = [condition1, condition2, condition3]

A, B, C = [f(c) for c in conditions]

# now we can get back the values as A[0] and A[1] instead of A_value1 and A_value2:
A_value1, A_value2 = A

请注意,我建议不要首先构建condition1。在变量名中包含数字几乎总是更好,因为具有一个名称的数组。