我正在编写一个代码,该代码将为我提供每月的进度报告,以显示我的债务应在哪里以及还清债务需要多长时间。当支付了一笔债务时,我想从数组中删除该值,并从各自的数组中删除相应的利率和最低还款额,这样我就可以提取花在这些债务上的值(钱)来还清其余的更快。有没有一种方法可以对每个数组中的每个值编制索引,以便说到还清865美元的“债务5”时,每个数组中与该债务对应的每个值都被删除了?当前,每个债务的每个详细信息在各自的数组中都位于相同的位置(即,债务1的名称为[0],债务的金额也为[0],其利息也为[0],依此类推)
import numpy as np
from numpy import round
# Max amount you can spend per month on debts
budget = 1000
# name your debts, balances, interests, and minimum payments
name = np.array(["Debt 1:", "Debt 2:", "Debt 3:", "Debt 4:", "Debt 5:", "Debt 6:"])
debt = np.array([250,300,7215,5500,864,1066])
interest = np.array([.1715,.0376,.0376,.0445,.0445,0])
minPayment = np.array([15,60,60,60,60,70])
# Miscellaneous variables for data storage and counting
index = [0]
intAccrued = []
month = []
i = 0
j = 0
# Loop through to calculate debt progress
while any(debt >=0):
debt = debt - minPayment + debt*interest/12
debt[debt <=0] = 0
## This deletes the zero values from the debt index, now I need a way to delete
## the corresponding minPayments and interest values from the other arrays
debt = np.delete(debt, index)
print (round(debt,2))
if sum(debt) == 0:
break
month
i += 1
print (i, "Months")
答案 0 :(得分:1)
与使用numpy
和使用pandas相比,我会走得更远。 pandas
有一个DataFrame
,基本上是一个表。您可以一次对整行或整列进行操作,例如通过在某种条件下过滤数据帧(此处为df[df.debt > 0]
):
import pandas as pd
df = pd.DataFrame({"name": name, "debt": debt, "interest": interest, "minimum_payment": minPayment}).set_index("name")
# debt interest minimum_payment
# name
# Debt 1: 250 0.1715 15
# Debt 2: 300 0.0376 60
# Debt 3: 7215 0.0376 60
# Debt 4: 5500 0.0445 60
# Debt 5: 864 0.0445 60
# Debt 6: 1066 0.0000 70
那么您的循环将是:
from itertools import count
for i in count(1):
df.debt = df.debt - df.minimum_payment + df.debt * df.interest / 12
df = df[df.debt > 0]
if len(df) == 0:
break
print(f"Month {i}:")
print(df)
print()
哪个给出以下输出:
# Month 1:
# debt interest minimum_payment
# name
# Debt 1: 215.226480 0.1715 15
# Debt 2: 122.264256 0.0376 60
# Debt 3: 7102.469139 0.0376 60
# Debt 4: 5380.746359 0.0445 60
# Debt 5: 692.979363 0.0445 60
# Debt 6: 856.000000 0.0000 70
#
# Month 2:
# debt interest minimum_payment
# name
# Debt 1: 203.302425 0.1715 15
# Debt 2: 62.647351 0.0376 60
# Debt 3: 7064.723542 0.0376 60
# Debt 4: 5340.699960 0.0445 60
# Debt 5: 635.549162 0.0445 60
# Debt 6: 786.000000 0.0000 70
#
# ...
#
# Month 149:
# debt interest minimum_payment
# name
# Debt 3: 8.948222 0.0376 60
答案 1 :(得分:0)
我建议在这里使用该类,并创建该类的引用数组。 在这里,我有一个示例刚刚尝试过。
#creating a class with the required structure.
class Expence:
name = ""
dept = 0.0
intrest = 0.0
minPayment = 0
def __init__(self,name,dept,intrest,minPayment):#here you can create methods to store the value to the class variables.
self.name = name
self.dept = dept
self.intrest = intrest
self.minPayment = minPayment
def getData(self):
return self.name, self.dept, self.intrest, self.minPayment
newExpence1 = Expence("Debt 1:",250,0.1715,15)
newExpence2 = Expence("Debt 2:",300,0.0376,60)
newExpence3 = Expence("Debt 3:",7215,0.0376,60)
expArray = [newExpence1,newExpence2,newExpence3] #adding element to array
print expArray[0].getData()[0] # access array elements with index value.
expArray.remove(newExpence1) # remove array elements with index value.
print expArray[0].getData()[0]
希望它会对您有所帮助。