Skip to main content
 首页 » 编程设计

python之在 python 中映射一组值

2025年05月04日11Leo_wl

执行以下操作的最佳方法是什么:

if lang=="en-US": 
    chapter="chapter" 
if lang=="fr-FR": 
    chapter="chapitre" 
# etc. 

本质上,我正在寻找一个可以执行以下操作的映射:

lang = ['en-US', 'fr-FR', ...] --> chapter = ['chapter', 'chapitre', ...] 

我将如何做到这一点?谢谢。

请您参考如下方法:

我总是使用字典。

langs = { 
  "en-US" : "chapter", 
  "fr-FR" : "chapitre" 
} 
 
test = langs['en-US'] 

但对于翻译,您还应该考虑 gettext .