【发布时间】:2021-08-04 15:41:12
【问题描述】:
在下面的json数据中,我需要得到问题和选项的组合(你可以在“选项”中找到它们:[])。 共有 3 个问题,第一个有 2 个选项,其余两个有 3 个选项,总共应该有 2 x 3 x 3 = 18 种组合。是否有功能可以执行或需要编写循环? 我需要在 python 中完成。
组合示例:
- Q1 C1 + Q2 C1 + Q3 C1
- Q1 C2 + Q2 C1 + Q3 C1
- Q1 C1 + Q2 C2 + Q3 C1 ...
它们的结果应该类似于这些:
- 颜色:红色,材质:金属,饰面:哑光
- 颜色:绿色,材质:金属,饰面:哑光
- 颜色:红色,材质:橡胶,表面处理:哑光
...
{
"Success": true,
"Options": [
{
"OptionId": 123,
"Question": "Color:",
"Choices": [
{
"ChoiceId": 333,
"Choice": "red",
"Url": "color/red/",
"Exceptions": []
},
{
"ChoiceId": 334,
"Choice": "green",
"Url": "color/green/",
"Exceptions": []
}
]
},
{
"OptionId": 223,
"Question": "material:",
"Choices": [
{
"ChoiceId": 223,
"Choice": "metal",
"Url": "material/metal/",
"Exceptions": []
},
{
"ChoiceId": 227,
"Choice": "rubber",
"Url": "material/rubber/",
"Exceptions": []
},
{
"ChoiceId": 229,
"Choice": "glass",
"Url": "material/glass/",
"Exceptions": []
}
]
},
{
"OptionId": 123,
"Question": "finish:",
"Choices": [
{
"ChoiceId": 123,
"Choice": "matte",
"Url": "finish/matte/",
"Exceptions": []
},
{
"ChoiceId": 123,
"Choice": "glossy",
"Url": "finish/glossy/",
"Exceptions": []
},
{
"ChoiceId": 123,
"Choice": "mix",
"Url": "finish/mix/",
"Exceptions": []
}
]
}
]
}
我有超过 10k 的 json 数据,每个都有不同数量的问题/选择,这只是一个例子。
感谢您的帮助。
【问题讨论】:
-
itertools.product() -
这看起来很有帮助。那么第一步是先将选择提取到列表中?
-
抱歉混淆,刚刚更正了描述部分。
标签: python json combinations