如何用 python 中同一数组中的另一个值替换数组值?

发布于 2025-01-12 14:54:26 字数 2268 浏览 4 评论 0原文

我将 Excel 工作表中的 37 行数据输入到 Python 中的这段代码中。 我试图通过计算同一数组中的数组中的某些值来增加值,然后用结果填充数组。我已经走到这一步了,

需要弄清楚如何将第二组括号中的 beginBalance 替换为前一组括号中的yearlyTotal。我需要不断地执行此操作,以便yearlTotal不断增加并推向下一个beginningBalance

另外,“beginningBalance”不必在currentRow中进行硬编码,但它必须从1500开始

import pprint


def getResults():
    mylist = []
    for n in range(1, 37 + 1):
        currentRow = {"age": 23 + n, "numOfYears": n,
                      "beginningBalance": 1500, "currentSalary": 72_000.00,
                      "dividendsAndGrowth": n, "yearlyDeposit": 8_640.00,
                      "yearlyTotal": n}

        mylist.append(currentRow)

        for x in mylist:
            x["dividendsAndGrowth"] = x["beginningBalance"] * 0.02

        for x in mylist:
            x["yearlyTotal"] = x["dividendsAndGrowth"] + x["yearlyDeposit"] + x["beginningBalance"]


    return mylist


if __name__ == "__main__":
    z = getResults()
    pprint.pprint(z)

这是一个示例,所以我希望从第一个开始的yearlyTotal括号成为第二个括号的开始余额,依此类推,

但正如您所看到的,结果在每个括号中重复出现,但年龄除外。

我尝试使用 for 循环实现 if 语句和/或在 for 循环末尾设置 x["beginningBalance"] = x["yearlyTotal"] 但没有任何效果。有什么想法吗?谢谢!

[{'age': 24,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 1,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 25,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 2,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 26,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 3,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},

我想要的输出样本

[{'age': 24,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 1,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 25,
  'beginningBalance': 10170.0,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 203.40,
  'numOfYears': 2,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 19,013.40},
 {'age': 26,
  'beginningBalance': 19,013.40,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 380.27,
  'numOfYears': 3,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 28,033.67},

I'm feeding 37 lines of data from an excel sheet to this code in python.
I'm trying to increment the values by calculating certain values from the array within the same array and then populating the array with the results. I have gotten this far

Need to figure out how to replace the beginningBalance in the second set of brackets with the yearlyTotal from the previous brackets. I need to do this continuously so that the yearlTotal keeps increasing and pushing on to the next beginningBalance

Also "beginningBalance" doesn't have to be hardcoded in currentRow but it has to start wit 1500

import pprint


def getResults():
    mylist = []
    for n in range(1, 37 + 1):
        currentRow = {"age": 23 + n, "numOfYears": n,
                      "beginningBalance": 1500, "currentSalary": 72_000.00,
                      "dividendsAndGrowth": n, "yearlyDeposit": 8_640.00,
                      "yearlyTotal": n}

        mylist.append(currentRow)

        for x in mylist:
            x["dividendsAndGrowth"] = x["beginningBalance"] * 0.02

        for x in mylist:
            x["yearlyTotal"] = x["dividendsAndGrowth"] + x["yearlyDeposit"] + x["beginningBalance"]


    return mylist


if __name__ == "__main__":
    z = getResults()
    pprint.pprint(z)

Here's an example so I would like yearlyTotal from the first bracket to become the beginningBalance of the second bracket and so on

but as you can see the results just repeat themselves in each bracket, with the exception of age.

I tried implementing if statements with the for loops and/or setting x["beginningBalance"] = x["yearlyTotal"] at the end of the for loops but nothing is working. Any ideas? Thanks!

[{'age': 24,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 1,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 25,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 2,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 26,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 3,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},

sample of output I want

[{'age': 24,
  'beginningBalance': 1500,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 30.0,
  'numOfYears': 1,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 10170.0},
 {'age': 25,
  'beginningBalance': 10170.0,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 203.40,
  'numOfYears': 2,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 19,013.40},
 {'age': 26,
  'beginningBalance': 19,013.40,
  'currentSalary': 72000.0,
  'dividendsAndGrowth': 380.27,
  'numOfYears': 3,
  'yearlyDeposit': 8640.0,
  'yearlyTotal': 28,033.67},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2025-01-19 14:54:26

问题是您没有更新循环内的beginingBalance。

一个好的做法是减少正在执行的 for 循环数量(循环大小为 37 时,这并不那么重要,但在考虑执行时间时它不会很好地扩展)。我建议这样做:

mylist = []
beginningBalance = 1500
for n in range(1, 37 + 1):
    currentRow = {"age": 23 + n,
                  "numOfYears": n,
                  "beginningBalance": beginningBalance,
                  "currentSalary": 72_000.00,
                  "dividendsAndGrowth": n,
                  "yearlyDeposit": 8_640.00,
                  "yearlyTotal": n}
    # This can be elsewhere but might as well be here for not using redundant loops
    currentRow["dividendsAndGrowth"] = currentRow["beginningBalance"] * 0.02
    currentRow["yearlyTotal"] = currentRow["dividendsAndGrowth"] + currentRow["yearlyDeposit"] + currentRow["beginningBalance"]
    # Saving the yearly total in another variable so that the cumulative yearly total will increase
    beginningBalance = currentRow["yearlyTotal"]
    mylist.append(currentRow)

return mylist

此外,我忘了提到,如果您知道循环将执行的迭代次数,建议预先分配内存,然后运行循环,因为创建一个空列表并附加到当循环中的迭代次数不断增加时,每次迭代的扩展性都会很差。

运行上面的代码时,结果列表为:

{'age': 24, 'numOfYears': 1, 'beginningBalance': 1500, 'currentSalary': 72000.0, 'dividendsAndGrowth': 30.0, 'yearlyDeposit': 8640.0, 'yearlyTotal': 10170.0}
{'age': 25, 'numOfYears': 2, 'beginningBalance': 10170.0, 'currentSalary': 72000.0, 'dividendsAndGrowth': 203.4, 'yearlyDeposit': 8640.0, 'yearlyTotal': 19013.4}
{'age': 26, 'numOfYears': 3, 'beginningBalance': 19013.4, 'currentSalary': 72000.0, 'dividendsAndGrowth': 380.26800000000003, 'yearlyDeposit': 8640.0, 'yearlyTotal': 28033.668}
{'age': 27, 'numOfYears': 4, 'beginningBalance': 28033.668, 'currentSalary': 72000.0, 'dividendsAndGrowth': 560.67336, 'yearlyDeposit': 8640.0, 'yearlyTotal': 37234.341360000006}
{'age': 28, 'numOfYears': 5, 'beginningBalance': 37234.341360000006, 'currentSalary': 72000.0, 'dividendsAndGrowth': 744.6868272000002, 'yearlyDeposit': 8640.0, 'yearlyTotal': 46619.028187200005}
{'age': 29, 'numOfYears': 6, 'beginningBalance': 46619.028187200005, 'currentSalary': 72000.0, 'dividendsAndGrowth': 932.3805637440001, 'yearlyDeposit': 8640.0, 'yearlyTotal': 56191.40875094401}
{'age': 30, 'numOfYears': 7, 'beginningBalance': 56191.40875094401, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1123.82817501888, 'yearlyDeposit': 8640.0, 'yearlyTotal': 65955.23692596289}
{'age': 31, 'numOfYears': 8, 'beginningBalance': 65955.23692596289, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1319.1047385192578, 'yearlyDeposit': 8640.0, 'yearlyTotal': 75914.34166448214}
{'age': 32, 'numOfYears': 9, 'beginningBalance': 75914.34166448214, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1518.2868332896428, 'yearlyDeposit': 8640.0, 'yearlyTotal': 86072.62849777179}
{'age': 33, 'numOfYears': 10, 'beginningBalance': 86072.62849777179, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1721.4525699554358, 'yearlyDeposit': 8640.0, 'yearlyTotal': 96434.08106772722}
{'age': 34, 'numOfYears': 11, 'beginningBalance': 96434.08106772722, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1928.6816213545444, 'yearlyDeposit': 8640.0, 'yearlyTotal': 107002.76268908176}
{'age': 35, 'numOfYears': 12, 'beginningBalance': 107002.76268908176, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2140.055253781635, 'yearlyDeposit': 8640.0, 'yearlyTotal': 117782.8179428634}
{'age': 36, 'numOfYears': 13, 'beginningBalance': 117782.8179428634, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2355.656358857268, 'yearlyDeposit': 8640.0, 'yearlyTotal': 128778.47430172068}
{'age': 37, 'numOfYears': 14, 'beginningBalance': 128778.47430172068, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2575.569486034414, 'yearlyDeposit': 8640.0, 'yearlyTotal': 139994.0437877551}
{'age': 38, 'numOfYears': 15, 'beginningBalance': 139994.0437877551, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2799.880875755102, 'yearlyDeposit': 8640.0, 'yearlyTotal': 151433.9246635102}
{'age': 39, 'numOfYears': 16, 'beginningBalance': 151433.9246635102, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3028.678493270204, 'yearlyDeposit': 8640.0, 'yearlyTotal': 163102.60315678042}
{'age': 40, 'numOfYears': 17, 'beginningBalance': 163102.60315678042, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3262.0520631356085, 'yearlyDeposit': 8640.0, 'yearlyTotal': 175004.65521991602}
{'age': 41, 'numOfYears': 18, 'beginningBalance': 175004.65521991602, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3500.0931043983205, 'yearlyDeposit': 8640.0, 'yearlyTotal': 187144.74832431434}
{'age': 42, 'numOfYears': 19, 'beginningBalance': 187144.74832431434, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3742.8949664862866, 'yearlyDeposit': 8640.0, 'yearlyTotal': 199527.64329080062}
{'age': 43, 'numOfYears': 20, 'beginningBalance': 199527.64329080062, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3990.5528658160124, 'yearlyDeposit': 8640.0, 'yearlyTotal': 212158.19615661664}
{'age': 44, 'numOfYears': 21, 'beginningBalance': 212158.19615661664, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4243.163923132333, 'yearlyDeposit': 8640.0, 'yearlyTotal': 225041.36007974896}
{'age': 45, 'numOfYears': 22, 'beginningBalance': 225041.36007974896, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4500.82720159498, 'yearlyDeposit': 8640.0, 'yearlyTotal': 238182.18728134394}
{'age': 46, 'numOfYears': 23, 'beginningBalance': 238182.18728134394, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4763.643745626879, 'yearlyDeposit': 8640.0, 'yearlyTotal': 251585.83102697082}
{'age': 47, 'numOfYears': 24, 'beginningBalance': 251585.83102697082, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5031.716620539416, 'yearlyDeposit': 8640.0, 'yearlyTotal': 265257.54764751025}
{'age': 48, 'numOfYears': 25, 'beginningBalance': 265257.54764751025, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5305.1509529502055, 'yearlyDeposit': 8640.0, 'yearlyTotal': 279202.6986004604}
{'age': 49, 'numOfYears': 26, 'beginningBalance': 279202.6986004604, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5584.053972009208, 'yearlyDeposit': 8640.0, 'yearlyTotal': 293426.75257246965}
{'age': 50, 'numOfYears': 27, 'beginningBalance': 293426.75257246965, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5868.535051449393, 'yearlyDeposit': 8640.0, 'yearlyTotal': 307935.287623919}
{'age': 51, 'numOfYears': 28, 'beginningBalance': 307935.287623919, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6158.70575247838, 'yearlyDeposit': 8640.0, 'yearlyTotal': 322733.9933763974}
{'age': 52, 'numOfYears': 29, 'beginningBalance': 322733.9933763974, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6454.679867527949, 'yearlyDeposit': 8640.0, 'yearlyTotal': 337828.6732439254}
{'age': 53, 'numOfYears': 30, 'beginningBalance': 337828.6732439254, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6756.573464878507, 'yearlyDeposit': 8640.0, 'yearlyTotal': 353225.24670880387}
{'age': 54, 'numOfYears': 31, 'beginningBalance': 353225.24670880387, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7064.504934176078, 'yearlyDeposit': 8640.0, 'yearlyTotal': 368929.75164298}
{'age': 55, 'numOfYears': 32, 'beginningBalance': 368929.75164298, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7378.5950328596, 'yearlyDeposit': 8640.0, 'yearlyTotal': 384948.34667583957}
{'age': 56, 'numOfYears': 33, 'beginningBalance': 384948.34667583957, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7698.966933516792, 'yearlyDeposit': 8640.0, 'yearlyTotal': 401287.31360935635}
{'age': 57, 'numOfYears': 34, 'beginningBalance': 401287.31360935635, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8025.746272187127, 'yearlyDeposit': 8640.0, 'yearlyTotal': 417953.0598815435}
{'age': 58, 'numOfYears': 35, 'beginningBalance': 417953.0598815435, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8359.06119763087, 'yearlyDeposit': 8640.0, 'yearlyTotal': 434952.1210791744}
{'age': 59, 'numOfYears': 36, 'beginningBalance': 434952.1210791744, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8699.042421583488, 'yearlyDeposit': 8640.0, 'yearlyTotal': 452291.1635007579}
{'age': 60, 'numOfYears': 37, 'beginningBalance': 452291.1635007579, 'currentSalary': 72000.0, 'dividendsAndGrowth': 9045.823270015158, 'yearlyDeposit': 8640.0, 'yearlyTotal': 469976.9867707731}

The problem is you are not updating the begginingBalance inside the loop.

A good practice will be to reduce the number of for loops you are performing (with loops size of 37 it is not that important but it will not scale well when looking at execution time). I would suggest doing something like this:

mylist = []
beginningBalance = 1500
for n in range(1, 37 + 1):
    currentRow = {"age": 23 + n,
                  "numOfYears": n,
                  "beginningBalance": beginningBalance,
                  "currentSalary": 72_000.00,
                  "dividendsAndGrowth": n,
                  "yearlyDeposit": 8_640.00,
                  "yearlyTotal": n}
    # This can be elsewhere but might as well be here for not using redundant loops
    currentRow["dividendsAndGrowth"] = currentRow["beginningBalance"] * 0.02
    currentRow["yearlyTotal"] = currentRow["dividendsAndGrowth"] + currentRow["yearlyDeposit"] + currentRow["beginningBalance"]
    # Saving the yearly total in another variable so that the cumulative yearly total will increase
    beginningBalance = currentRow["yearlyTotal"]
    mylist.append(currentRow)

return mylist

In addition, I forgot to mention that if you know the number of iterations your loop will perform, it is recommended to pre-allocate memory and then run the loop, since creating an empty list and appending to it at each iteration will scale poorly when working with growing number of iteration over the loop.

When running the code above the resulted list was:

{'age': 24, 'numOfYears': 1, 'beginningBalance': 1500, 'currentSalary': 72000.0, 'dividendsAndGrowth': 30.0, 'yearlyDeposit': 8640.0, 'yearlyTotal': 10170.0}
{'age': 25, 'numOfYears': 2, 'beginningBalance': 10170.0, 'currentSalary': 72000.0, 'dividendsAndGrowth': 203.4, 'yearlyDeposit': 8640.0, 'yearlyTotal': 19013.4}
{'age': 26, 'numOfYears': 3, 'beginningBalance': 19013.4, 'currentSalary': 72000.0, 'dividendsAndGrowth': 380.26800000000003, 'yearlyDeposit': 8640.0, 'yearlyTotal': 28033.668}
{'age': 27, 'numOfYears': 4, 'beginningBalance': 28033.668, 'currentSalary': 72000.0, 'dividendsAndGrowth': 560.67336, 'yearlyDeposit': 8640.0, 'yearlyTotal': 37234.341360000006}
{'age': 28, 'numOfYears': 5, 'beginningBalance': 37234.341360000006, 'currentSalary': 72000.0, 'dividendsAndGrowth': 744.6868272000002, 'yearlyDeposit': 8640.0, 'yearlyTotal': 46619.028187200005}
{'age': 29, 'numOfYears': 6, 'beginningBalance': 46619.028187200005, 'currentSalary': 72000.0, 'dividendsAndGrowth': 932.3805637440001, 'yearlyDeposit': 8640.0, 'yearlyTotal': 56191.40875094401}
{'age': 30, 'numOfYears': 7, 'beginningBalance': 56191.40875094401, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1123.82817501888, 'yearlyDeposit': 8640.0, 'yearlyTotal': 65955.23692596289}
{'age': 31, 'numOfYears': 8, 'beginningBalance': 65955.23692596289, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1319.1047385192578, 'yearlyDeposit': 8640.0, 'yearlyTotal': 75914.34166448214}
{'age': 32, 'numOfYears': 9, 'beginningBalance': 75914.34166448214, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1518.2868332896428, 'yearlyDeposit': 8640.0, 'yearlyTotal': 86072.62849777179}
{'age': 33, 'numOfYears': 10, 'beginningBalance': 86072.62849777179, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1721.4525699554358, 'yearlyDeposit': 8640.0, 'yearlyTotal': 96434.08106772722}
{'age': 34, 'numOfYears': 11, 'beginningBalance': 96434.08106772722, 'currentSalary': 72000.0, 'dividendsAndGrowth': 1928.6816213545444, 'yearlyDeposit': 8640.0, 'yearlyTotal': 107002.76268908176}
{'age': 35, 'numOfYears': 12, 'beginningBalance': 107002.76268908176, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2140.055253781635, 'yearlyDeposit': 8640.0, 'yearlyTotal': 117782.8179428634}
{'age': 36, 'numOfYears': 13, 'beginningBalance': 117782.8179428634, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2355.656358857268, 'yearlyDeposit': 8640.0, 'yearlyTotal': 128778.47430172068}
{'age': 37, 'numOfYears': 14, 'beginningBalance': 128778.47430172068, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2575.569486034414, 'yearlyDeposit': 8640.0, 'yearlyTotal': 139994.0437877551}
{'age': 38, 'numOfYears': 15, 'beginningBalance': 139994.0437877551, 'currentSalary': 72000.0, 'dividendsAndGrowth': 2799.880875755102, 'yearlyDeposit': 8640.0, 'yearlyTotal': 151433.9246635102}
{'age': 39, 'numOfYears': 16, 'beginningBalance': 151433.9246635102, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3028.678493270204, 'yearlyDeposit': 8640.0, 'yearlyTotal': 163102.60315678042}
{'age': 40, 'numOfYears': 17, 'beginningBalance': 163102.60315678042, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3262.0520631356085, 'yearlyDeposit': 8640.0, 'yearlyTotal': 175004.65521991602}
{'age': 41, 'numOfYears': 18, 'beginningBalance': 175004.65521991602, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3500.0931043983205, 'yearlyDeposit': 8640.0, 'yearlyTotal': 187144.74832431434}
{'age': 42, 'numOfYears': 19, 'beginningBalance': 187144.74832431434, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3742.8949664862866, 'yearlyDeposit': 8640.0, 'yearlyTotal': 199527.64329080062}
{'age': 43, 'numOfYears': 20, 'beginningBalance': 199527.64329080062, 'currentSalary': 72000.0, 'dividendsAndGrowth': 3990.5528658160124, 'yearlyDeposit': 8640.0, 'yearlyTotal': 212158.19615661664}
{'age': 44, 'numOfYears': 21, 'beginningBalance': 212158.19615661664, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4243.163923132333, 'yearlyDeposit': 8640.0, 'yearlyTotal': 225041.36007974896}
{'age': 45, 'numOfYears': 22, 'beginningBalance': 225041.36007974896, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4500.82720159498, 'yearlyDeposit': 8640.0, 'yearlyTotal': 238182.18728134394}
{'age': 46, 'numOfYears': 23, 'beginningBalance': 238182.18728134394, 'currentSalary': 72000.0, 'dividendsAndGrowth': 4763.643745626879, 'yearlyDeposit': 8640.0, 'yearlyTotal': 251585.83102697082}
{'age': 47, 'numOfYears': 24, 'beginningBalance': 251585.83102697082, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5031.716620539416, 'yearlyDeposit': 8640.0, 'yearlyTotal': 265257.54764751025}
{'age': 48, 'numOfYears': 25, 'beginningBalance': 265257.54764751025, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5305.1509529502055, 'yearlyDeposit': 8640.0, 'yearlyTotal': 279202.6986004604}
{'age': 49, 'numOfYears': 26, 'beginningBalance': 279202.6986004604, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5584.053972009208, 'yearlyDeposit': 8640.0, 'yearlyTotal': 293426.75257246965}
{'age': 50, 'numOfYears': 27, 'beginningBalance': 293426.75257246965, 'currentSalary': 72000.0, 'dividendsAndGrowth': 5868.535051449393, 'yearlyDeposit': 8640.0, 'yearlyTotal': 307935.287623919}
{'age': 51, 'numOfYears': 28, 'beginningBalance': 307935.287623919, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6158.70575247838, 'yearlyDeposit': 8640.0, 'yearlyTotal': 322733.9933763974}
{'age': 52, 'numOfYears': 29, 'beginningBalance': 322733.9933763974, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6454.679867527949, 'yearlyDeposit': 8640.0, 'yearlyTotal': 337828.6732439254}
{'age': 53, 'numOfYears': 30, 'beginningBalance': 337828.6732439254, 'currentSalary': 72000.0, 'dividendsAndGrowth': 6756.573464878507, 'yearlyDeposit': 8640.0, 'yearlyTotal': 353225.24670880387}
{'age': 54, 'numOfYears': 31, 'beginningBalance': 353225.24670880387, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7064.504934176078, 'yearlyDeposit': 8640.0, 'yearlyTotal': 368929.75164298}
{'age': 55, 'numOfYears': 32, 'beginningBalance': 368929.75164298, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7378.5950328596, 'yearlyDeposit': 8640.0, 'yearlyTotal': 384948.34667583957}
{'age': 56, 'numOfYears': 33, 'beginningBalance': 384948.34667583957, 'currentSalary': 72000.0, 'dividendsAndGrowth': 7698.966933516792, 'yearlyDeposit': 8640.0, 'yearlyTotal': 401287.31360935635}
{'age': 57, 'numOfYears': 34, 'beginningBalance': 401287.31360935635, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8025.746272187127, 'yearlyDeposit': 8640.0, 'yearlyTotal': 417953.0598815435}
{'age': 58, 'numOfYears': 35, 'beginningBalance': 417953.0598815435, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8359.06119763087, 'yearlyDeposit': 8640.0, 'yearlyTotal': 434952.1210791744}
{'age': 59, 'numOfYears': 36, 'beginningBalance': 434952.1210791744, 'currentSalary': 72000.0, 'dividendsAndGrowth': 8699.042421583488, 'yearlyDeposit': 8640.0, 'yearlyTotal': 452291.1635007579}
{'age': 60, 'numOfYears': 37, 'beginningBalance': 452291.1635007579, 'currentSalary': 72000.0, 'dividendsAndGrowth': 9045.823270015158, 'yearlyDeposit': 8640.0, 'yearlyTotal': 469976.9867707731}
独留℉清风醉 2025-01-19 14:54:26

您的值不断重复的原因是因为您的 beginBalance 在循环内始终为 1500,这是一种方法,您可以根据前一行的当前行并递增它。

def getResults():
    my_list = []
    starting_row = {
        "age": 24,
        "numOfYears": 1,
        "beginningBalance": 1500,
        "currentSalary": 72_000.00,
        "dividendsAndGrowth": 30.0,
        "yearlyDeposit": 8640.00,
        "yearlyTotal": 10170.0
    }

    my_list.append(starting_row)
    for n in range(1, 37):
        previous_row = my_list[n - 1]
        current_row = previous_row.copy()
        current_row["age"] = previous_row["age"] + 1
        current_row["numOfYears"] = previous_row["numOfYears"] + 1
        current_row["beginningBalance"] = previous_row["yearlyTotal"]
        current_row["dividendsAndGrowth"] = current_row["beginningBalance"] * 0.02
        current_row["yearlyTotal"] = current_row["dividendsAndGrowth"] + current_row["yearlyDeposit"] + current_row["beginningBalance"]
        my_list.append(current_row)

    return my_list

The reason why your values keep repeating is because your beginningBalance is always 1500 inside the loop, here is a way you can do it basing the current row on the previous row and incrementing it.

def getResults():
    my_list = []
    starting_row = {
        "age": 24,
        "numOfYears": 1,
        "beginningBalance": 1500,
        "currentSalary": 72_000.00,
        "dividendsAndGrowth": 30.0,
        "yearlyDeposit": 8640.00,
        "yearlyTotal": 10170.0
    }

    my_list.append(starting_row)
    for n in range(1, 37):
        previous_row = my_list[n - 1]
        current_row = previous_row.copy()
        current_row["age"] = previous_row["age"] + 1
        current_row["numOfYears"] = previous_row["numOfYears"] + 1
        current_row["beginningBalance"] = previous_row["yearlyTotal"]
        current_row["dividendsAndGrowth"] = current_row["beginningBalance"] * 0.02
        current_row["yearlyTotal"] = current_row["dividendsAndGrowth"] + current_row["yearlyDeposit"] + current_row["beginningBalance"]
        my_list.append(current_row)

    return my_list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文