有没有更好的方法来写这个?

时间:2010-10-28 22:34:28

标签: python

有更好的方法吗?我觉得我因为过于重复而做错了。

O = viz.pick(1, viz.WORLD)

BackSetts = ["set_b1b", "set_b2a", "set_b1a",  "set_b2b"]
LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"]
NormSetts = ["set_nr_a", "set_nr_b"]
Maps = ["MapA","MapB"]

if O.name in BackSetts:
    for i in set(BackSetts)|set(Maps):
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

elif O.name in LeftSetts:
    for i in set(LeftSetts)|set(Maps):
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

elif O.name in NormSetts:
    for i in NormSetts:
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

1 个答案:

答案 0 :(得分:3)

琐碎的转变是:

O = viz.pick(1, viz.WORLD)

BackSetts = ["set_b1b", "set_b2a", "set_b1a",  "set_b2b"]
LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"]
NormSetts = ["set_nr_a", "set_nr_b"]
Maps = ["MapA","MapB"]
anyset = []

if O.name in BackSetts:
    anyset = set(BackSetts)|set(Maps)

elif O.name in LeftSetts:
    anyset = set(LeftSetts)|set(Maps)

elif O.name in NormSetts:
    anyset = NormSetts

for i in anyset:
    WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

这样可以使NormSetts不与地图联合,就像在原始代码中一样。