迭代输入场景并将结果存储为Python中的嵌套数组

2024-05-12 13:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用一个循环为所有3个输入场景运行我的模型,而不必复制粘贴脚本3次并手动更改输入数据。我有3个输入数据数组,并希望将结果(同样长度的数组)存储在同一变量中的独立嵌套数组中。目前,我只知道如何附加结果。但是,这是不正确的,我想将不同场景运行的结果存储在同一变量中的不同元素中

import numpy as np
# Scenarios
years = np.arange(50)
sc0 = np.arange(50)
sc1 = np.arange(50)+100
sc2 = np.arange(50)+200

scenarios = [sc0, sc1, sc2]

results = [] 

# Model computes something
for sc in range(3):
    for t in years:
        outcome = scenarios[sc][t] / 10
        results.append(outcome)

简而言之,解决方案应该允许我使用results[0]results[1]results[2]访问所有模型运行的结果


Tags: 数据in模型fornp场景数组results
2条回答

理解也可以:

resultsets = [[sc[t]/10 for t in years] for sc in scenarios]

我已经创建了一个新的列表subresults,它被创建为每个场景的[]。然后,在为该场景计算每个结果之后,将其附加到列表results

import numpy as np
# Scenarios
years = np.arange(50)
sc0 = np.arange(50)
sc1 = np.arange(50)+100
sc2 = np.arange(50)+200

scenarios = [sc0, sc1, sc2]

results = []

# Model computes something
for sc in range(3):
    subresults = [] 
    for t in years:
        outcome = scenarios[sc][t] / 10
        subresults.append(outcome)
    results.append(subresults)

然后使用results[0]results[1]results[2]访问结果

相关问题 更多 >