如何从函数返回修改后的矩阵?

我需要创建一个函数,该函数将嵌套的货币+转换率列表作为参数。然后计算出每种货币将购买多少美元10、20、30、40、50、60、70、80、90和100美元。

因此,如果传递给函数的矩阵是:

[['AUD', 0.96],['USD', 0.75],['Euro', 0.67],['GBP', 0.496]]

输出应如下所示:

[[10, 9.6, 7.5, 6.7, 4.96], [20, 19.2, 15.0, 13.4, 9.92], [30, 28.799999999999997, 22.5, 20.1, 14.879999999999999], [40, 38.4, 30.0, 26.8, 19.84], [50, 48.0, 37.5, 33.5, 24.8], [60, 57.599999999999994, 45.0, 40.2, 29.759999999999998], [70, 67.2, 52.5, 46.900000000000006, 34.72], [80, 76.8, 60.0, 53.6, 39.68], [90, 86.39999999999999, 67.5, 60.300000000000004, 44.64], [100, 96.0, 75.0, 67.0, 49.6]]

我想出了以下代码:

def makeTable(lst):
res =[[]]
matrix = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
conversion = [item[1] for item in lst]

for num in m:
    res.append(num)
    for rate in convers:
        res.append(num * rate)
return (res)

result = makeTable([['AUD', 0.96], ['USD',0.75], ['Euro',0.67], ['GBP',0.496]])
print (result)

但这并没有给我输出作为我试图获取的嵌套列表的信息。谁能告诉我如何更改代码,以便返回的矩阵是子列表的列表,每个子列表包含$(10-100,以10为增量),后跟4个不同的转换。

评论
云想衣裳
云想衣裳

也许这是有帮助的:

def makeTable(lst): 
    res =[] 
    matrix = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] 
    conversion = [item[1] for item in lst]

    for num in matrix: 
        m=[num,]

        for rate in conversion: 
            m.append(num * rate)
        res.append(m)
    return (res) 

result = makeTable([['AUD', 0.96], ['USD',0.75], ['Euro',0.67], ['GBP',0.496]]) 
print (result)

对于3个数字的极限浮点数,您可以替换:

m.append(num * rate)

带有:

m.append(round(num * rate,3))
点赞
评论