是否有一个python函数来获得json数据的组合?



在下面的json数据中,我需要获得问题和选择的组合(您可以在"Options":[]中找到它们)。有3个问题,第一个有2个选择,其余两个有3个选择,总共应该有2 × 3 × 3 = 18种组合。是否有一个函数来完成它,或者需要写一个循环?我需要在python中执行.

结合例子:

  1. Q1C1+ q2 c1 + q3 c1
  2. Q1C2+ q2 c1 + q3 c1
  3. q1 c1 + q2c2+ q3 c1…

它们的结果应该与以下相似:

  1. 颜色:,材质:金属,光洁度:哑光
  2. 绿色颜色
  3. :,材质:金属,光洁度:哑光
  4. 颜色:红色,材质:橡胶, finish:哑光

……

{
"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数据,每个数据都有不同数量的问题/选择,这只是一个例子。

谢谢你的帮助。

是否有一个python函数来获得json数据的组合?

据我所知,没有现成的函数可以直接获取JSON数据,但是首先将JSON加载到Python对象中,然后将它们组合起来并不难。

import json
o = json.loads("""
{
"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": []
}
]
}
]

}              """)
# now we have the JSON data in the Python object o
questions = [oo["Question"] for oo in o["Options"]]
# now we have the questions = ['Color:', 'material:', 'finish:']
choices = [[c["Choice"] for c in oo["Choices"]] for oo in o["Options"]]
# now we have the choices = [['red', 'green'],
#                            ['metal', 'rubber', 'glass'],
#                            ['matte', 'glossy', 'mix']]
# a well-known recursive algorithm for the cartesian product
# (could instead use itertools.product() as suggested)
def products(cs):
if cs:
for c in cs[-1]:
for p in products(cs[:-1]): yield p+[c]
else: yield []
cnt = 0
for p in products(choices): # count and print each result in the desired format
cnt += 1
print("%d."%cnt, ", ".join([" ".join(qc) for qc in zip(questions, p)]))

最新更新