python 中关于先打印列再打印行的循环问题

For loop question in python about printing the columns first then printing the row

我有一个程序可以帮助我计算一些值。 而这个输出的结果就像一个三角形。

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 columns
        num = (1 + S[j -1])**j      # Calculating x by a certain formula
        den = (1 + S[i - 1])**i     # Calculating x by a certain formula
        x = (num/den)**(1/(j - i)) -1       # Calculating x by a certain formula and get the value of x
        print('{:0.4f}'.format(x), end=' ') # print all the values 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 

但是如果我只得到第一列(运行我的程序后的结果)作为我的输入,那就是

0.0600 
0.0690 
0.0750 
0.0801 
0.0841 
0.0857 
0.0904

如何通过修改原始代码和公式的逆运算来恢复整个table(即整个三角形)?我需要在原始代码上添加一个新的for循环吗?


换句话说, 如果我得到一个公式 x = ((((1 + k[j - 1])**j) / ((1 + k[i - 1])i)) (1/(j-i))) - 1 ,在程序中。 然后,让我们假设我可以得到

的结果
1 2 3     #the numbers (1 - 6) represents the order of being printed
4 5
6

但是我怎样才能将三角形的格式从上面的模式更改为模式

1 4 6     #the numbers (1 - 6) represents the order of being printed
2 5
3

通过公式的反运算?

笼统地说:

  1. 不是打印每一行,而是将该行的每个条目保存到列表中
  2. 不是在外循环中打印新行,而是创建一个新列表并将前一个保存在另一个“结果”列表中。
  3. 遍历此 two-dimensional 列表、反转、排序或其他任何内容,然后按您喜欢的方式打印

更具体地说:

from itertools import zip_longest

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

n = len(S)

results = []
results += [S]

for i in range(1,n):                # loop over rows
    line = []
    for j in range(i+1, n+1):       # then over columns
        num = (1 + S[j -1])**j      # Calculating x by a certain formula
        den = (1 + S[i - 1])**i     # Calculating x by a certain formula
        x = (num/den)**(1/(j - i)) -1       # Calculating x by a certain formula and get the value of x
        line += [x]
    results += [line]


for line in results:
    for entry in line:
        print(entry, end='')
    print('')

print("-" * 10)
for line in zip_longest(*results):
    for entry in line:
        if entry is not None:
            print(entry, end=' ')
    print('')
0.060.06450.0680.0710.07360.07560.0777
0.069019103773584780.072022613109848480.074691975296487630.077027177187337430.07874744277738710.08067860239842739
0.075034561087013340.077539689995302920.079709843727620910.081193329041452820.08302571799174219
0.080050656549259140.082055104262303670.083254082397003830.08503277149878619
0.084063271995895580.084859356567028140.08669857831554517
0.085656025744853540.08801863286709821
0.09038638150035694
----------
0.06 0.06901910377358478 0.07503456108701334 0.08005065654925914 0.08406327199589558 0.08565602574485354 0.09038638150035694
0.0645 0.07202261310984848 0.07753968999530292 0.08205510426230367 0.08485935656702814 0.08801863286709821
0.068 0.07469197529648763 0.07970984372762091 0.08325408239700383 0.08669857831554517
0.071 0.07702717718733743 0.08119332904145282 0.08503277149878619
0.0736 0.0787474427773871 0.08302571799174219
0.0756 0.08067860239842739
0.0777

我会把数字格式留给你。