2010-10-20 82 views
1

我有這樣的:另一個列表理解問題

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 

是否有可能進一步簡化這一點,使之成爲一個班輪?

+1

是的簡化,沒有列表理解 – nearlymonolith 2010-10-20 00:28:18

回答

3
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"]]) 
+0

我不知道.index,謝謝。它的工作原理和保持我的列表一樣。 – relima 2010-10-20 00:31:50

+0

+1索引是這裏最好的解決方案。使用字典會過度殺傷。 – 2010-10-20 00:43:28

+0

我會創建這樣的字典:'Linguas = dict([(lang,i)for i,lang in enumerate([「en」,「pt」,「fr」,「es」]))' – hughdbrown 2010-10-20 01:23:01

3
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"]])