简体   繁体   中英

how to create a dynamic dictionary with lists in it using JSON python?

I have dictionary formate which contain 4 lists and in the end, I want to convert it into JSON.

modelOutput = {
        "projectYears": 1,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": []
            }, {
                "name": "variableSavings",
                "variableSavingsCost": []
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": []
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": []
            },
        ],
}

SO if the projectYear value is 2 then there will be 2 dictionaries with 4 lists(Cost) with 2 random values in it in each dictionary. expected output:

#if projectYear = 2
modelOutput = {
        "projectYears": 1,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": [12,22]
            }, {
                "name": "variableSavings",
                "variableSavingsCost": [11,15]
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": [32,21]
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": [21,33]
            },
        ],
"projectYears": 2,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": [32,23]
            }, {
                "name": "variableSavings",
                "variableSavingsCost": [23,12]
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": [14,32]
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": [14,13]
            },
        ],

Similarly, if projectYears is 3 then there will 3 dictionaries with 4 lists and 3 values in each of them. I was able to create the random values in the lists according to the projectYears but can't able to form separate dictionaries out of it. My Approach:

projectLife = 3


modelOutput['RevSev'][0]['fixedSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['variableSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['startUpSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['shutDownSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]

json.dumps(modelOutput)

Here's a fairly straightforward way of doing that, given modelOutput like the above as input (or even given just the number of years you'd like to have):

years = modelOutput["projectYears"]

randoms = lambda y: np.random.randint(10, 30, y)

res = []
for year in range(1, years+1):
    new_year = {
        "projectYears": year, 
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": randoms(years)
            }, {
                "name": "variableSavings",
                "variableSavingsCost": randoms(years)
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": randoms(years)
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": randoms(years)
            }
        ]
    }
    res.append(new_year)

The result for '2' is:

[{'projectYears': 1,
  'RevSev': [{'name': 'FixedSavings', 'fixedSavingsCost': array([24, 22])},
   {'name': 'variableSavings', 'variableSavingsCost': array([11, 12])},
   {'name': 'startUpSavings', 'startUpSavingsCost': array([27, 22])},
   {'name': 'shutDownSavings', 'shutDownSavingsCost': array([25, 17])}]},
 {'projectYears': 2,
  'RevSev': [{'name': 'FixedSavings', 'fixedSavingsCost': array([15, 19])},
   {'name': 'variableSavings', 'variableSavingsCost': array([20, 13])},
   {'name': 'startUpSavings', 'startUpSavingsCost': array([26, 22])},
   {'name': 'shutDownSavings', 'shutDownSavingsCost': array([24, 25])}]}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM