与列表的输出对齐问题

2024-09-30 01:35:05 发布

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

我试图得到一个左对齐的输出..但我不断得到对齐问题。这是我的密码:

def print_report(symbol:str,strategy:str, alist1:list, alist2:list, alist3:list, alist4:list):
    print('\nSYMBOL:   ', symbol.upper())
    print('STRATEGY: ', strategy)
    print('\n''DATE            CLOSING        INDICATOR        SIGNAL')
    for i in range(0,len(alist1)):
        print('{0:<0}{1:>13}{2:>15}{3:>15}'.format(alist1[i],alist2[i],alist3[i],alist4[i]))

输出应如下所示:

DATE          CLOSING     INDICATOR       SIGNAL
2013-10-01     887.00        0.00               
2013-10-02     887.99        0.00               
2013-10-03     876.09        0.00               
2013-10-04     872.35        0.00               
2013-10-07     865.74      877.83         SELL
2013-10-08     853.67      871.17         SELL
2013-10-09     855.86      864.74         SELL
2013-10-10     868.24      863.17          BUY
2013-10-11     871.99      863.10          BUY
2013-10-18    1011.41      911.27          BUY
2013-10-21    1003.30      936.71          BUY

有没有更简单的方法来获取输出?或者正确对齐


Tags: datesignalbuysymbollistindicatorprintclosing
1条回答
网友
1楼 · 发布于 2024-09-30 01:35:05

以与数据行相同的格式打印标题

def print_report(symbol:str, strategy:str,
                 dates:list, closings:list, indicators:list, signals:list):
    fmt = '{0[0]:<0}{0[1]:>13}{0[2]:>15}{0[3]:>15}'
    print()
    print('SYMBOL:   ', symbol.upper())
    print('STRATEGY: ', strategy)
    print()
    print(fmt.format(('DATE', 'CLOSING', 'INDICATOR', 'SIGNAL')))
    for d in zip(dates, closings, indicators, signals):
        print(fmt.format(d))

如果您愿意,可以为头使用不同的格式,但关键是,也可以为头使用format方法

相关问题 更多 >

    热门问题