我有一个字典和一个清单,如下。
>>> new_coins
{'main': '100', 'single': '10'}
>>> get_snip_priority('prod-oregon')
['prod-singapore', 'prod-delhi']
我需要使用上述2种数据结构来创建字典的最有效方法,如下所示:
{
main : {
'prod-singapore': 100, # 100 is the value of main
'prod-delhi': 100 },
single: {
'prod-singapore': 10, # 10 is the value of static
'prod-delhi': 10 },
能帮上忙吗
答案 0 :(得分:5)
您可以使用嵌套的dict理解:
new_coins = {'main': '100', 'single': '10'}
cities = ['prod-singapore', 'prod-delhi']
new = {key:{city:value for city in cities} for key,value in new_coins.items()}
print (new)
#{'main': {'prod-singapore': '100', 'prod-delhi': '100'}, 'single': {'prod-singapore': '10', 'prod-delhi': '10'}}