简体   繁体   English

将循环函数输出存储到列表中

[英]Storing Loop Function Outputs into a List

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}.')

OUTPUT:输出:

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.

Now I would like to create a list that stores these values.现在我想创建一个存储这些值的列表。 The list should read [1000000, 1000000, 1000000, 1000000, 1000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]该列表应为 [1000000, 1000000, 1000000, 1000000, 1000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I tried creating an empty list and then appending it, but I can't seem to figure out how to get it to output the desired list I showed above.我尝试创建一个空列表然后附加它,但我似乎无法弄清楚如何让它输出我上面显示的所需列表。 Any tips?有小费吗?

HERES THE DATA CLASS I AM WORKING WITH这是我正在使用的数据类

@dataclass
class ModelInputs:
    n_phones: float = 100000
    price_scrap: float = 50000
    price_phone: float = 2000
    cost_machine_adv: float = 1000000
    cogs_phone: float = 250
    n_life: int = 10
    n_machines: int = 5
    d_1: float = 100000
    g_d: float = 0.2
    max_year: float = 20
    interest: float = 0.05

    # Inputs for bonus problem
    elasticity: float = 100
    demand_constant: float = 300000

data = ModelInputs()
data
[match_exp_at_year(ModelInputs, year) if year < data.n_machines else 0 for year in range(data.max_year)]

Can't reproduce your output, but in general below is an example of appending values to a list in Python's for loop:无法重现您的输出,但一般而言,下面是将值附加到 Python 的 for 循环中的列表的示例:

my_list = []
for i in range(0,100):
    my_list.append(i)

Just above your first loop, declare and initialise an empty list:就在你的第一个循环之上,声明并初始化一个空列表:

myList = []

Immediately under your first print statement (and in the loop) add the line:紧接在您的第一个打印语句下(并在循环中)添加以下行:

myList.append(mach_exp_t)

and then under your second print statement (and in the loop):然后在您的第二个打印语句下(并在循环中):

myList.append(0)

Print the result right at the very bottom with:在最底部打印结果:

print ( myList )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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