【问题标题】:Is there a python function to get the combinations of json data?是否有 python 函数来获取 json 数据的组合?
【发布时间】:2021-08-04 15:41:12
【问题描述】:

在下面的json数据中,我需要得到问题和选项的组合(你可以在“选项”中找到它们:[])。 共有 3 个问题,第一个有 2 个选项,其余两个有 3 个选项,总共应该有 2 x 3 x 3 = 18 种组合。是否有功能可以执行或需要编写循环? 我需要在 python 中完成

组合示例:

  1. Q1 C1 + Q2 C1 + Q3 C1
  2. Q1 C2 + Q2 C1 + Q3 C1
  3. Q1 C1 + Q2 C2 + Q3 C1 ...

它们的结果应该类似于这些:

  1. 颜色:红色,材质:金属,饰面:哑光
  2. 颜色:绿色,材质:金属,饰面:哑光
  3. 颜色:红色,材质:橡胶,表面处理:哑光

...

{
    "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


【解决方案1】:

有没有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)]))

【讨论】:

  • 谢谢!这很有帮助
猜你喜欢
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
相关资源
最近更新 更多