我想撰寫一個程式,可以幫助我使用 python 計算一些值。主要思想是我想制作一個表格,其中只有一半的單元格具有價值。(也就是說我要制作一個三角形)所以,我要制作的三角形就是這樣的格式。
12345
1234
123
12
1
但是,事情并沒有那么容易。請參閱下表。(也與圖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
在上面的三角形中,每一行都是通過某個公式從前一行組成的。請參考圖2和圖3。
另外,請注意,在上面的三角形中,每個單元格都乘以 100。例如,左上角的 6.00 確實是 6% (0.06)。<< 請不要在我的程式中將數字乘以 100,因為這只是為了演示而發生的意外。
好的。所以,我想做一個程式,只要我輸入第一行,程式就會為我計算并顯示剩余的行。(這意味著顯示整個三角形)
下面是我的代碼。請看一看。我試圖寫一個框架,但是我遇到了兩個問題。
- 我不知道如何根據我在程式中輸入的第一個陣列(第一行)自動填充我的其他陣列(其他行)。
- 當我嘗試運行我的程式時,我不斷收到“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
感謝您閱讀我的問題。我想請人幫我完成這個程式。如您所見,我的編碼可能很糟糕,因為我是編程新手。但我已經盡了最大的努力去做我能做的一切。
我不介意您編輯我的代碼。如果你能為我寫一個新的代碼就更好了。我可以向你學習如何撰寫更好的代碼。
最后,我還有一個要求。請在您撰寫的代碼中添加許多注釋,否則我可能無法理解您寫的內容(我是初學者)。非常感謝你!
uj5u.com熱心網友回復:
你想多了。
如果您只需要列印一個三角形,只需按順序列印每一行,并為每一行按順序列印其值:
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
現在,如果您想首先構建陣列,因為您稍后將對其進行后處理,只需將每個欄位值附加到該行的串列中,然后將每個串列附加到串列的全域串列中
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
uj5u.com熱心網友回復:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424823.html
