Home > Back-end >  How do I replace array value with another value from same array in python?
How do I replace array value with another value from same array in python?

Time:03-09

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},

CodePudding user response:

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}

CodePudding user response:

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
  • Related