开发者

Yet another list comprehension question

I had this:

    if Setting["Language"] == "en":
        f.m_radioBox3.SetSelection(0)
    elif Setting["Language"] == "pt":
        f.m_radioBox3.SetSelection(1)
    elif Setting["Language"] == "fr":
        f.m_radioBox3.SetSelection(2)
    elif Setting["Language"] == "es":
        f.m_radioBox3.SetSelection(开发者_如何转开发3)

Then I did this:

    Linguas = ["en","pt","fr","es"]
    a = 0
    for i in Linguas:
        if i == Setting["Language"]:
            f.m_radioBox3.SetSelection(a)
        a += 1

Is it possible to further simplify this and make it into a one-liner?


Linguas = ["en","pt","fr","es"]

if Setting["Language"] in Linguas:
    f.m_radioBox3.SetSelection(Linguas.index(Setting["Language"]))

or you could do it with a dictionary:

Linguas = {"en":0,"pt":1,"fr":2,"es":3}

if Setting["Language"] in Linguas:
    f.m_radioBox3.SetSelection(Linguas[Setting["Language"]])


mapping = {"en" : 0, "pt" : 1, "fr" : 2, "es" : 3}
if Setting["Language"] in mapping:
    f.m_radioBox3.SetSelection(mapping[Setting["Language"]])

If you don't need to check for the setting being one of an acceptable number of values, it becomes:

mapping = {"en" : 0, "pt" : 1, "fr" : 2, "es" : 3}
f.m_radioBox3.SetSelection(mapping[Setting["Language"]])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜