我在Python 2.7中有以下元组列表和字典
lambdatuple = [(8.0,), (4.0,), (5.0,), (8.0, 4.0), (8.0, 5.0), (4.0, 5.0), (8.0, 4.0, 5.0)]
muscenarios = {0: 240.0, 1: 600.0, 2: 480.0, 3: 300.0, 4: 297.1, 5: 526.8, 6: 337.1}
基于lambdatuple中每个元素的元组长度,我想在muscenarios中复制我的元素,使其成为:
muscenarios = {0: 240.0, 1: 600.0, 2: 480.0, 3: 300.0, 4: 300.0, 5: 297.1, 6: 297.1,
7: 526.8, 8: 526.8, 9:337.1, 10: 337.1, 11: 337.1}
答案 0 :(得分:0)
好的,没关系。我找到了解决方案。以下代码执行了我尝试执行的操作:
muscenariosnew = []
for j in range(len(lambdatuple)):
muscenariosnew.append([muscenarios.values()[j]] * len(lambdatuple[j]))
muscenariosnew = [item for sublist in muscenariosnew for item in sublist]
muscenarios
[240.0,
600.0,
480.0,
300.0,
300.0,
297.1,
297.1,
526.8,
526.8,
337.1,
337.1,
337.1]