我需要附加两个词典的键,并将需求乘以时间需求分布。
样本数据(实际数据要大得多):
dict = {'S-NSW-BAC-ENG': 15, 'S-NSW-BAC-FBE': 30}
temporal_demand_distibution = {-60: 0.001187, -59: 0.001354}
预期的输出类似于:
resulting_dict = {'S-NSW-BAC-ENG:-60': 0.001187*15, 'S-NSW-BAC-ENG:-59': 0.001354*15, 'S-NSW-BAC-FBE:-60': 30*0.001187, 'S-NSW-BAC-FBE:-59': 30*0.001354}
值显然是乘法的结果,而不是乘法本身。
我知道在没有示例代码的情况下发布问题很糟糕,但我不知道如何实现这一目标。
答案 0 :(得分:2)
使用double for循环并组合键和值:
from pprint import pprint
dict = {'S-NSW-BAC-ENG': 15, 'S-NSW-BAC-FBE': 30}
temporal_demand_distribution = {-60: 0.001187, -59: 0.001354}
result_dict = {}
for key, value in dict.items():
for key2, value2 in temporal_demand_distribution.items():
result_dict["{}:{}".format(key, key2)] = value * value2
pprint(result_dict)
{'S-NSW-BAC-ENG:-59': 0.020309999999999998, 'S-NSW-BAC-ENG:-60': 0.017804999999999998, 'S-NSW-BAC-FBE:-59': 0.040619999999999996, 'S-NSW-BAC-FBE:-60': 0.035609999999999996}
答案 1 :(得分:0)
试试这个:
dict = {'S-NSW-BAC-ENG': 15, 'S-NSW-BAC-FBE': 30}
temporal_demand_distibution = {-60: 0.001187, -59: 0.001354}
output = {}
for k, v in dict.items():
for i, j in temporal_demand_distibution.items():
output[k + ':' + str(i)] = v * j
print(output)
或者带字典理解的单行内容:
output = {'%s:%d' % (elem, num): val * decimal for elem, val in dict.items() for num, decimal in temporal_demand_distibution.items()}
答案 2 :(得分:0)
如果你想使用dict理解,试试这个:
dojo.require