使用循环在 python 中通过数组制作直角三角形并自动填充数组

Making a right triangle by arrays in python using loops and auto filling the arrays

我想编写一个程序来帮助我使用 python 计算一些值。 主要想法是我想制作一个 table,其中只有一半的单元格具有价值。 (也就是说我要做一个三角形)所以,我要做的三角形就是这样的格式。

12345
1234
123
12
1

但是,事情并没有那么容易。请参阅下面的 table。 (也与 picture 1 相同)

6.00 6.45 6.80 7.10 7.36 7.56 7.77
6.90 7.20 7.47 7.70 7.88 8.06
7.50 7.75 7.97 8.12 8.30
8.00 8.20 8.33 8.50
8.40 8.50 8.67
8.60 8.80
9.00

在上面的三角形中,每一行都是根据特定公式从上一行生成的。请参考picture 2 and picture 3.

另外,请注意,在上面的三角形中,每个单元格都乘以 100。例如,左上角的 6.00 确实是 6%(0.06)。 <<请不要在我的程序中将数字乘以100,因为这只是为了演示而发生的意外。

好的。所以,我想做一个程序,只要我输入第一行,程序就会为我计算并显示剩余的行。 (这意味着显示整个三角形)

下面是我的代码。请看一看。我试过写一个框架,但是遇到了两个问题

  1. 我不知道如何根据我在程序中输入的第一个数组(第一行)自动填充我的其他数组(其他行)。
  2. 当我尝试 运行 我的程序时,我不断收到“IndexError:列表索引超出范围”的错误。

这是我的代码

array1 = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]         #This is the initial array that I input
array2 = []            #This should be the output after the first iteration, I hope that my programme can auto fill the remaining arrays (including this array) after I run my programme
array3 = []            #This is a blank array waiting for auto fill after I run my program
array4 = []            #This is a blank array waiting for auto fill after I run my program
array5 = []            #This is a blank array waiting for auto fill after I run my program
array6 = []            #This is a blank array waiting for auto fill after I run my program
array7 = []            #This is a blank array waiting for auto fill after I run my program
array8 = []            #This is a blank array waiting for auto fill after I run my program


r = 0  # Set the initial position for the table that I want to make, and this is similar to the x-asis. Please refer to picture 2.
s = 1  # Set the initial position for the table that I want to make, and this is similar to the y-asis. Please refer to picture 2.



def SpotRateForcast(i,j,k):
    global r
    global s
    
    while (r <= len(k)):                                     # I try to make the first loop here
        s = 1                                                # Reset the value when the first row is completed such that the second loop can work in the second row
        while (s <= len(k)):                                 # this is the second loop to determine when to stop in the each row
            x = ((((1 + k[j - 1])**j) / ((1 + k[i - 1])**i))**(1/(j-i))) - 1  #Calculate the value by the certain formula
            print(("%.4f" % round(x,4)), end=' ,  ')         #Print the value up to 4 decimal places  
            s += 1                                           #Record one value is calculated and done
            SpotRateForcast(i,j+1,k)                         #Start to calculate and print the next value                         
        r += 1                                               # Switch to the next row when the second loop is finished
        
        
SpotRateForcast(r,s,array1)                                  #The code to run the program     

感谢您阅读我的问题。 我想请人帮我完成程序。如您所见,我的编码可能很糟糕,因为我是编程新手。但是我已经尽力做到了我能做的一切

我不介意你修改我的代码。如果你能帮我写一个新的代码就更好了。而且我可以向你学习如何编写更好的代码。

最后,我还有一个要求。请在您编写的代码中添加许多注释,否则我可能无法理解您写的内容(我是初学者)。非常感谢!

array=[[0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]]

n = len(array[0])

#outer loop

for i in range(1, n):

       #append new row

    array.append([])
    
    for j in range(n-i):
        #i dont understand your calculations but they'd stand
        array[i][j] = #here

你想多了

如果您只需要打印一个三角形,只需按顺序打印每一行,然后按顺序打印每一行的值:

S = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]

n = len(S)

print(('{:0.4f} ' * n).format(*S))  # print initial line

for i in range(1,n):                # loop over rows
    for j in range(i+1, n+1):       # then over colums
        num = (1 + S[j -1])**j
        den = (1 + S[i - 1])**i
        x = (num/den)**(1/(j - i)) -1       # ok we have f(i, j)
        print('{:0.4f}'.format(x), end=' ') # print it followed with a space 
    print('')                       # add a newline after a full row

它给出:

0.0600 0.0645 0.0680 0.0710 0.0736 0.0756 0.0777 
0.0690 0.0720 0.0747 0.0770 0.0787 0.0807 
0.0750 0.0775 0.0797 0.0812 0.0830 
0.0801 0.0821 0.0833 0.0850 
0.0841 0.0849 0.0867 
0.0857 0.0880 
0.0904 

现在,如果您想首先构建数组,因为您稍后会 post-process 它们,只需将每个字段值附加到该行的列表,然后将这些列表中的每一个附加到列表的全局列表

S = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]

n = len(S)

result = [S]                        # add the initial row

for i in range(1,n):                # loop over rows
    row = []                            # prepare a new list for the row
    result.append(row)                  # and append it to result
    for j in range(i+1, n+1):       # then over colums
        num = (1 + S[j -1])**j
        den = (1 + S[i - 1])**i
        x = (num/den)**(1/(j - i)) -1       # ok we have f(i, j)
        row.append(x)               # as a list is modifiable we can append to it