【问题标题】:Creating a List of lists in Python在 Python 中创建列表列表
【发布时间】:2018-10-12 14:34:31
【问题描述】:

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

例如,输入 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)

任何指导将不胜感激:)

【问题讨论】:

  • 注意输入x在while循环中不可用。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解您的代码,但关于您的迷你示例,您想要计算 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 次数,结果被放入一个列表中,这个列表被添加到另一个列表nb_simulations 次数。

    【讨论】:

      【解决方案3】:
      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'都会自动重置,您不需要在while中指定if条件,因为条件已经存在于while中

      【讨论】:

      • 和原来的问题一样,第一行中定义的 x 变量在 while 循环中是不可访问的。
      • 嘿,我想改变它,但问题是它仍然只是一遍又一遍地打印相同的第一个列表。这是因为输入在每次循环后都没有重置吗?我猜“x”在范围内应该是一个不同的字母
      • @Quickbeam2k1 如果我们想访问在第一行定义的 x 变量,我们可以使用 globals()['x'] 来获取该变量的值
      猜你喜欢
      • 1970-01-01
      • 2018-08-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      相关资源
      最近更新 更多