如何用 python 中同一数组中的另一个值替换数组值?
我将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有更新循环内的beginingBalance。
一个好的做法是减少正在执行的 for 循环数量(循环大小为 37 时,这并不那么重要,但在考虑执行时间时它不会很好地扩展)。我建议这样做:
此外,我忘了提到,如果您知道循环将执行的迭代次数,建议预先分配内存,然后运行循环,因为创建一个空列表并附加到当循环中的迭代次数不断增加时,每次迭代的扩展性都会很差。
运行上面的代码时,结果列表为:
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:
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:
您的值不断重复的原因是因为您的 beginBalance 在循环内始终为 1500,这是一种方法,您可以根据前一行的当前行并递增它。
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.