在Python中创建列表列表

时间:2018-05-02 13:04:42

标签: python list for-loop while-loop append

我正在研究金融计算器。我目前拥有它,以便每次代码循环时它都会添加到结果列表中。我打算将这些结果添加到新列表中,以便输入循环的次数。 修改函数将确保第一个列表在每个循环中具有不同的结果,因为它使通货膨胀和利率随机化。

例如,输入sims为10,因此它将循环10次。每次循环时,它都会将列表结果添加到列表模拟中,然后重置列表结果。这将产生10个不同的列表列表

目前我正在获取我需要的输出,但只有第一个循环被添加到第二个列表,所以例如它只重复第一个循环10次而不是每次重置和随机化。

def modify_rate(interest_rate, interest_change):

   return interest_rate + interest_change * (1 - random.random() * 2)

def modify_rate(inflation_rate, inflation_change):

   return inflation_rate + inflation_change * (1 - random.random() * 2)

def run_simulation():
years, annual_spend, savings_balance, inflation_rate, interest_rate, interest_change, inflation_change, sims  = inputs()
simulations = []
i = 0
 for x in range(0, sims):
    result = []
    while i < years:
        interest_rate = modify_rate(interest_rate, interest_change)
        inflation_rate = modify_rate(inflation_rate, inflation_change)
        i = i + 1
        totalcost = annual_spend * (1 + inflation_rate) ** 1
        annual_spend = totalcost
        totalsave = savings_balance - annual_spend
        savings_balance = totalsave
        x = savings_balance * (1 + interest_rate) ** 1
        savings_balance = round(x, 2)
        result.append(savings_balance)
        if i >=years:
            break
        elif savings_balance < 0:
            break
        simulations.append(result)
print(simulations)

我创建了一个小型的例子,其中计算将被添加x次,然后将结果添加到列表中,然后将其添加到另一个列表中x次

x = int(input("enter"))
y = int(input("enter"))
k = int(input("enter"))
a = []
i = 0
h= []
for x in range(0, k):
while i < y:
            i = i + 1
            z = x + 10 + i
            a.append(z)
            if i >= y:
                break
h.append(a)  
print(h)

任何指导将不胜感激:)

3 个答案:

答案 0 :(得分:1)

x = int(input("enter"))
y = int(input("enter"))
k = int(input("enter"))
h= []
for x in range(0, k):
    a = []
    i = 0
    while i < y:
            i += 1
            z = x + 10 + i
            a.append(z)
    h.append(a)  
print(h)

每次for循环执行列表'a'都会自动重置,并且您不需要指定条件是否在条件已经存在的同时

答案 1 :(得分:0)

我不确定我是否完全了解您的代码,但就您的迷你示例而言,您希望进行def _pre_put_hook(self): raise RuntimeError('Cannot put entity {}.'.format(type(self).__name__)) 次计算,将结果放入列表中,此列表将添加到另一个列出y次。

k

当然,base_value = int(input("enter")) # your x nb_calculations = int(input("enter")) # your y nb_simulations = int(input("enter")) # your k final_list= [] # your h for value in range(nb_simulations): print('working on simulation: ', value) temp_list = [] # your a i = 0 # your i while i < nb_calculations: print('working on calculation: ', i) new_value = base_value + 10 + i temp_list.append(new_value) print('base value: ', base_value) print('new value: ', new_value) print('temp list: ', temp_list) i += 1 print('final temp list: ', temp_list) print('len temp list equal to nb_calculations: ', len(temp_list) == nb_calculations) final_list.append(temp_list) print('final list: ', final_list) print('len final list: ', len(final_list)) print('final list at the end: ', final_list) print('len final list at the end: ', len(final_list)) print('len final list equal to nb_simulations: ', len(final_list) == nb_simulations) 将始终相同,因为计算总是相同的。但计算完成temp_list次,结果放在一个列表中,此列表会被添加到另一个列表中nb_calculations次。

答案 2 :(得分:0)

设法解决它,不确定它是否是最有效的方式,但无论如何不好发布

def run_simulation():
years, annual_spend, savings_balance, inflation_rate, interest_rate, interest_change, inflation_change, sims  = inputs()
simulations = []
loop = 0
for loop in range(0, sims):
    success = 0
    fail = 0
    initial1 = interest_rate
    initial2 = inflation_rate
    initial3 = annual_spend
    initial4 = savings_balance
    i = 0
    result = []
    while i < years:
        interest_rate = modify_rate(interest_rate, interest_change)
        inflation_rate = modify_rate(inflation_rate, inflation_change)
        i = i + 1
        totalcost = annual_spend * (1 + inflation_rate) ** 1
        annual_spend = totalcost
        totalsave = savings_balance - annual_spend
        savings_balance = totalsave
        x = savings_balance * (1 + interest_rate) ** 1
        savings_balance = round(x, 2)
        result.append(savings_balance)
        if i >=years:
            break
        elif savings_balance < 0:
            break
    simulations.append(result)
    inflation_rate = initial1
    interest_rate = initial2
    annual_spend = initial3
    savings_balance = initial4        
    loop =+1