我有一本字典和一个列表如下。
>>> 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 },
有人可以帮忙吗
您可以使用嵌套的字典理解:
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'}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句