将循环功能输出存储到列表中

问题描述 投票:0回答:1

INPUT

def mach_exp_at_year(data: ModelInputs, year):
    mach_exp_t = data.cost_machine_adv
    return mach_exp_t

for i in range(data.n_machines):
    year = i + 1
    mach_exp_t = mach_exp_at_year(ModelInputs, year)
    print(f'The machine expense at year {year} is ${mach_exp_t:,.0f}.')

for i in range(data.n_machines, data.max_year):
    year = i + 1
    print(f'The machine expense at year {year} is ${0:,.0f}.')

输出:

The machine expense at year 1 is $1,000,000.
The machine expense at year 2 is $1,000,000.
The machine expense at year 3 is $1,000,000.
The machine expense at year 4 is $1,000,000.
The machine expense at year 5 is $1,000,000.
The machine expense at year 6 is $0.
The machine expense at year 7 is $0.
The machine expense at year 8 is $0.
The machine expense at year 9 is $0.
The machine expense at year 10 is $0.
The machine expense at year 11 is $0.
The machine expense at year 12 is $0.
The machine expense at year 13 is $0.
The machine expense at year 14 is $0.
The machine expense at year 15 is $0.
The machine expense at year 16 is $0.
The machine expense at year 17 is $0.
The machine expense at year 18 is $0.
The machine expense at year 19 is $0.
The machine expense at year 20 is $0.

现在,我想创建一个存储这些值的列表。列表应读取为[1000000,1000000,1000000,1000000,1000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

我尝试创建一个空列表,然后附加它,但是我似乎无法弄清楚如何使其输出上面显示的所需列表。有提示吗?

python list loops for-loop
1个回答
0
投票

无法再现您的输出,但是下面通常是在Python的for循环中将值附加到列表的示例:


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.