Skip to main content
 首页 » 编程设计

python之迭代列表并将该值用作字典键

2024年10月17日9tuyile006

<分区>

我的字典看起来有些这样想。

allCurrencies = { 
    'AUD': ['Australian Dollar', 'au'], 
    'GBP': ['British Pound', 'gb'], 
    'CAD': ['Canadian Dollar', 'ca'], 
    'INR': ['Indian Rupee', 'in'], 
    'JPY': ['Japanese Yen', 'jp'], 
    'RUB': ['Russian Ruble', 'ru'], 
    'SGD': ['Singapore Dollar', 'sg'], 
    'CHF': ['Swiss Franc', 'ch'], 
    'USD': ['US Dollar', 'us'] 
} 

我的数组包含这个:

commonCurrencies = ['USD', 'EUR', 'GBP', 'JPY'] 

我的主要目标是迭代 commonCurrencies 并将其用作字典 allCurrencies 的键。

我的 django 模板目前看起来像这样:

<tr> 
    {% for sym in commonCurrencies %} 
        <td>{{allCurrencies.sym.0}}<td> 
    {% endfor %} 
</tr> 

但是好像不行。我究竟做错了什么。谢谢