我有两个字典,一个存储国家/地区名称,另一个存放相应的国家/地区lat,长坐标。我想将path_dict
的值替换为另一个字典中匹配键的值。
pathcoord_dict = {"AED": [25.30000,55.43330], "EUR": [50.85000,4.35000],
"JPY": [35.68330,139.73330], "CNY": [39.91660,116.38330],
"GBP": [51.50000,-0.11670], "CAD": [45.42000, -75.67000],
"BRL": [-23.56660,-46.6332], "AUD": [ -33.91660,151.28330],
"NZD": [-36.88320,174.75000], "CHF": [46.23330,6.0667],
"NOK": [59.93330, 10.68330], "SEK": [59.38330 ,18.00000],
"SGD": [1.36670,103.75000], "HKD": [21.75000,115.00000],
"CNY": [39.91660,116.38330], "INR": [18.96670,72.83330],
"RUB": [55.75000, 37.61670], "MXN": [ 19.46670,-99.14990],
"TRY": [41.03330,29.00000], "AED": [25.30000,55.43330],
"BRL": [-23.56660,-46.6332]}
path_dict = {'AUD': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'CHF': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'CNY': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'TRY': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'AED': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'GBP': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'NZD': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'CAD': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'MXN': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'NOK': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'RUB': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'INR': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'JPY': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']],
'BRL': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']],
'EUR': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']], 'SEK': [['USD'], ['JPY'], ['JPY'],
['EUR'], ['EUR'], ['INR'], ['INR'], ['USD']],
'SGD': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']],
'HKD': [['USD'], ['JPY'], ['JPY'], ['EUR'], ['EUR'], ['INR'],
['INR'], ['USD']]}
我试图让最终结果看起来像这样
{'CHF': [[35.68330,139.73330], [50.85000,4.35000], [18.96670,72.83330], etc], ...}
答案 0 :(得分:2)
循环遍历path_dict并获取值列表中每个元素的坐标。如下所示:
for k, v in path_dict.items():
coords = [pathcoord_dict[x[0]] for x in v]
path_dict[k] = coords
答案 1 :(得分:1)
如果添加了"USD"
字典中pathcoord_dict
的缺失条目,您可以使用所谓的dictionary comprehension表达式(或显示)执行此类操作:
result = {country: [pathcoord_dict[i[0]] for i in path_dict[country]]
for country in path_dict}
for country in result:
print '{!r}: {}'.format(country, result[country])
输出:
'TRY': [[0.0, 0.0], [35.6833, 139.7333], [35.6833, 139.7333], [50.85, 4.35],
[50.85, 4.35], [18.9667, 72.8333], [18.9667, 72.8333], [0.0, 0.0]]
'AUD': [[0.0, 0.0], [35.6833, 139.7333], [35.6833, 139.7333], [50.85, 4.35],
[50.85, 4.35], [18.9667, 72.8333], [18.9667, 72.8333], [0.0, 0.0]],
...