另一个列表理解问题

发布于 2024-09-28 09:09:27 字数 575 浏览 1 评论 0原文

我有这样的:

    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)

然后我这样做了:

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

是否有可能进一步简化它并将其变成一行行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-10-05 09:09:27
Linguas = ["en","pt","fr","es"]

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

或者你可以用字典来做到这一点:

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

if Setting["Language"] in Linguas:
    f.m_radioBox3.SetSelection(Linguas[Setting["Language"]])
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"]])
芯好空 2024-10-05 09:09:27
mapping = {"en" : 0, "pt" : 1, "fr" : 2, "es" : 3}
if Setting["Language"] in mapping:
    f.m_radioBox3.SetSelection(mapping[Setting["Language"]])

如果您不需要检查设置是否是可接受的值之一,则它将变为:

mapping = {"en" : 0, "pt" : 1, "fr" : 2, "es" : 3}
f.m_radioBox3.SetSelection(mapping[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"]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文