TransWikia.com

Iteration in Python throws results no desired

Stack Overflow Asked by Catalina Hernández on February 20, 2021

I am trying to assign payments which are stored in p vector to the vector of debts which is l, so I need to store the dates of payment and the payed value for each date of payment, according the debts.

This code is for the payments for only the first debt ($254.000) (the position 0 in l):[62998, datetime.date(2019, 11, 29), 254000.0]

The desired results are:

  1. fechas_rec=[datetime.date(2020, 1, 30), datetime.date(2020, 2, 28)] vector of payment dates
  2. p=[[10000.0, datetime.date(2020, 2, 28)], [110000.0, datetime.date(2020, 3, 30)]] update the payments with the amounts available.
  3. recuperados=[137500.0,116500.0] vector of payments according to the vector of the point 1.

But, when update the second last line "recuperados.append()" with the commented code "recuperados.append(‘a’)", the output of the first point change, the question is why?

The new output is:

fechas_rec=[datetime.date(2020, 2, 28), datetime.date(2020, 3, 30)]

Code:

l = [
    [62998, datetime.date(2019, 11, 29), 254000.0],
    [65317, datetime.date(2019, 12, 30), 229000.0],
    [66606, datetime.date(2020, 1, 31), 229000.0],
    [68751, datetime.date(2020, 2, 28), 228000.0],
    [70652, datetime.date(2020, 3, 24), 229000.0],
]

p = [
    [137500.0, datetime.date(2020, 1, 30)],
    [126500.0, datetime.date(2020, 2, 28)],
    [110000.0, datetime.date(2020, 3, 30)],
]


for j in l:
    fechas_rec = []
    recuperados = []
    suma = 0
    for i in enumerate(p):
        suma = suma + i[1][0]
        if suma <= j[2]:
            fechas_rec.append(i[1][1])
            recuperados.append(i[1][0])
        else:
            val = suma - j[2]
            c = i[1]
            del p[: i[0] + 1]
            p.insert(0, [val, c[1]])
            fechas_rec.append(i[1][1])
            recuperados.append()  # recuperados.append('a')
            break

One Answer

Code:

l = [
    [62998, datetime.date(2019, 11, 29), 254000.0],
    [65317, datetime.date(2019, 12, 30), 229000.0],
    [66606, datetime.date(2020, 1, 31), 229000.0],
    [68751, datetime.date(2020, 2, 28), 228000.0],
    [70652, datetime.date(2020, 3, 24), 229000.0],
]

p = [
    [137500.0, datetime.date(2020, 1, 30)],
    [126500.0, datetime.date(2020, 2, 28)],
    [110000.0, datetime.date(2020, 3, 30)],
]

for j in l:
    fechas_rec=[]
    recuperados=[]
    suma=0
    for i in enumerate(p):
        suma1=suma
        suma=suma+i[1][0]
        if suma<=j[2]:
            fechas_rec.append(i[1][1])
            recuperados.append(i[1][0])
        else:
            val=suma-j[2]
            c=i[1]
            del p[:i[0]+1]
            p.insert(0,[val,c[1]])
            fechas_rec.append(i[1][1])
            recuperados.append(j[2]-suma1) 
            break  
    drec.append([j[0],fechas_rec,recuperados])    
    sumtot=sumtot+sum(drec[-1][2])
    if sumtot==rectot:
        test=True       
    if test:
        break  

Answered by Catalina Hernández on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP