在python中列化循环输出

2024-09-30 08:26:01 发布

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

我在python中有这样一个代码:

def sth(mlist):
    for line in mlist:
        out = func(param1, param2)
        if out:
            print " [+] %-15s %20s " % (line, out.rjust(30, '.'))
        else:
            a = 'Not Covered!!'
            print " [-] %-15s %20s " % (target, a.rjust(30, '.'))

我在运行代码时得到了糟糕的结果:

 [-] http://a.com .................Not Covered!! 
 [-] http://abcd.info .................Not Covered!! 
 [+] http://abcdef.net ....................something  
 [-] https://c.com .................Not Covered!! 
 [+] https://efghij.org .................other thing
.
.
.

如何获得此类线程的最佳输出形式,例如:

 [-] http://a.com ......................... Not Covered!! 
 [-] http://abcd.info ..................... Not Covered!! 
 [+] http://abcdef.net .................... something  
 [-] https://c.com ........................ Not Covered!! 
 [+] https://efghij.org ................... other thing
.
.
.

注:

甚至其他解决方案,而不是使用ljust也很受欢迎。你知道吗


Tags: 代码httpsinfocomhttpnetlinenot
2条回答

您可以预先计算最长的URL,并用点对齐该条目,以便它更完美地配对:

>>> signs = ['-', '+', '-']  
... URLs = ['http://foobar.com', 'http://much_longer_foo.com', 'http://medium_foo.com']  
... tails = ['Longer shouting ending!!', 'different ending', 'Longer shouting ending!!'] 
...   
... maxlen = max(map(len, URLs))  
... linefmt = " [{{sign}}] {{URL:.<{maxlen}}}{{tail:.>30}}".format(maxlen=maxlen)  
... for sign,URL,tail in zip(signs, URLs, tails):  
...     print(linefmt.format(sign=sign, URL=URL, tail=tail)) 
...                                                                                                                                                                                                                                           
 [-] http://foobar.com...............Longer shouting ending!!
 [+] http://much_longer_foo.com..............different ending
 [-] http://medium_foo.com...........Longer shouting ending!!

如您所见,我改为使用.format而不是百分比格式。这主要是一个偏好的问题。你知道吗

重要的是,我们首先用最长URL的长度构建格式字符串,然后使用生成的格式字符串。结果与预期略有不同:圆点周围没有空格。如果您坚持要添加它们,可以手动添加它们。你知道吗

请注意,在Python3.6及更高版本中,可以使用f-strings更优雅地执行此操作:

>>> signs = ['-', '+', '-']  
... URLs = ['http://foobar.com', 'http://much_longer_foo.com', 'http://medium_foo.com']  
... tails = ['Longer shouting ending!!', 'different ending', 'Longer shouting ending!!'] 
...   
... maxlen = max(map(len, URLs))  
... for sign,URL,tail in zip(signs, URLs, tails):  
...     print(f' [{sign}] {URL:.<{maxlen}}{tail:.>30}')  
...                                                                                                                                                                                                                                           
 [-] http://foobar.com...............Longer shouting ending!!
 [+] http://much_longer_foo.com..............different ending
 [-] http://medium_foo.com...........Longer shouting ending!!

另外,如您所见,我建议使用一个格式字符串,并分别传递sign/URL/ending。这将帮助您减少代码重复并简化可维护性。你知道吗

好吧,由于您对不使用rjustljust的解决方案持开放态度,您可以决定要打印多少.,并从中减去linelen。你知道吗

简短示例:

print(' [+] {0} {1} {2}'.format(line, '.' * (50 - len(line)), out))

相关问题 更多 >

    热门问题