希望将公式集成到一个数组中并在单个数组中显示它(python)

2024-09-27 21:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

i = [2,3,4]
interest = [1.08,1.0824,1.09,1.095]

yr2 = (interest[0] * interest[1])**(1./i[0]) - 1.
yr3 = (interest[0] * interest[1] * interest[2])**(1./i[1]) - 1.
yr4 = (interest[0] * interest[1] * interest[2] * interest[3])**(1./i[2]) - 1.

我声明了2个数组(如上),但我希望以以下格式显示我的答案:

yield = [yr2,yr3,yr4] 

Tags: 答案声明格式数组yieldinterestyr2yr3
1条回答
网友
1楼 · 发布于 2024-09-27 21:34:03
# data
i = [2,3,4]
interest = [1.08,1.0824,1.09,1.095]

def count_interests(bar, interest):
    """
    Returns a list:
    [(interest[0] * interest[1]), (interest[0] * interest[1] * interest[2]),
    (interest[0] * interest[1] * interest[2] * interest[3])]
    """
    res = []
    compound = [interest[0]]
    print(f'first compound {compound}')
    for i in range(len(bar)): # [0,1,2]
        inter = compound[i] * interest[i+1]
        compound.append(inter)
    print(compound)
    return compound

compound = count_interests(i, interest)
compound.pop(0)
print(compound)
# [1.168992, 1.2742012800000002, 1.3952504016000002]
zipped = zip(i, compound)

def yr(zip):
    res = []
    for k, v in zip:
        temp = v ** (1./k) - 1
        res.append(temp)
    return res

result = yr(zipped)

结果

[0.0811993340730468, 0.08412496573593131, 0.08683355662001646]

相关问题 更多 >

    热门问题