【发布时间】:2020-03-20 21:34:35
【问题描述】:
我有一个包含每个键的列表元素的字典。例如。
config_dict = {"num":[2,3,4],
"dist":[10,30,22],
"type":["free"],
"uphill":[1e-3, 1e-2, 1e-6]
}
我想创建一个函数,它为每个键创建所有可能的元素子集作为新字典。例如
new_config = {'congfig_1': { "num":2,
"dist":22,
"type":"free",
"uphill":1e-3
}
'config_2': { "num":3,
"dist":22,
"type":"free",
"uphill":1e-6
}
'config_3': { "num":2,
"dist":10,
"type":"free",
"uphill":1e-3
}
...
}
我被困在实现上,因为每个键可能有不同长度的列表。有没有一种优雅的方式来做到这一点/没有很多循环的pythonic方式?
【问题讨论】:
-
将输出作为字典列表不是有意义吗?
-
是的。这也有效。我按照我想象的方式打字
标签: python-3.x list dictionary data-structures