[英]Aligning stdout print in python
I have the following program that uses a deque that I copied from the web. 我有以下程序使用从网络复制的双端队列。
from collections import Counter,deque
import re
import time
import my_ds
num = 100000
def append(c):
for i in range(num):
c.append(i)
def appendleft(c):
if isinstance(c, deque):
for i in range(num):
c.appendleft(i)
else:
for i in range(num):
c.insert(0,i)
def pop(c):
for i in range(num):
c.pop()
def popleft(c):
if isinstance(c,deque):
for i in range(num):
c.popleft()
else:
for i in range(num):
c.pop(0)
for container in [deque, list]:
for operation in [append, appendleft, pop, popleft]:
c = container(range(num))
start = time.time()
operation(c)
elapsed = time.time() - start
print('Completed {0}/{1} in {2} seconds: {3} ops/sec'.format(container.__name__,operation.__name__, elapsed, num/elapsed))
The output looks like below. 输出如下所示。
Completed deque/append in 0.011004447937011719 seconds: 9087234.595718866 ops/sec
Completed deque/appendleft in 0.00800323486328125 seconds: 12494947.56911344 ops/sec
Completed deque/pop in 0.00800323486328125 seconds: 12494947.56911344 ops/sec
Completed deque/popleft in 0.009003400802612305 seconds: 11106914.175250906 ops/sec
Completed list/append in 0.011004447937011719 seconds: 9087234.595718866 ops/sec
Completed list/appendleft in 8.727489709854126 seconds: 11458.048456601553 ops/sec
Completed list/pop in 0.01900768280029297 seconds: 5261030.555416185 ops/sec
Completed list/popleft in 1.781712532043457 seconds: 56125.776858800775 ops/sec
I am looking for a nice way to align the numbers seconds
and ops/second
.How is this done in python 我正在寻找一种很好的方式来对齐数字seconds
和ops/second
这是如何在python中完成的
如果您需要的位置不仅限于此,还可以使用制表模块,该模块将为您很好地设置格式,而无需事先猜测长度。
You need to align Completed deque/append
part as well for the rest of columns to be aligned. 您还需要对齐Completed deque/append
部分,以对齐其余的列。 Try this: 尝试这个:
print('Completed {0}/{1:<11} in {2:<20} seconds: {3:<18} ops/sec'.format(container.__name__,operation.__name__, elapsed, num/elapsed))
Completed deque/append in 0.009192228317260742 seconds: 10878755.025288548 ops/sec
Completed deque/appendleft in 0.008057117462158203 seconds: 12411386.636681067 ops/sec
Completed deque/pop in 0.009001970291137695 seconds: 11108679.18531663 ops/sec
Completed deque/popleft in 0.008355855941772461 seconds: 11967654.863469055 ops/sec
Completed list/append in 0.009819984436035156 seconds: 10183315.528794795 ops/sec
Completed list/appendleft in 5.856244802474976 seconds: 17075.78890106128 ops/sec
Completed list/pop in 0.012813091278076172 seconds: 7804517.8817312345 ops/sec
Completed list/popleft in 1.437035083770752 seconds: 69587.72345181856 ops/sec
You can use the < and > alignment operators of the format() string method. 您可以使用format()字符串方法的<和>对齐运算符。 Look at the documentation here: 在这里查看文档:
https://docs.python.org/3.5/library/string.html#format-string-syntax https://docs.python.org/3.5/library/string.html#format-string-syntax
For example: 例如:
>>> a='deque'
>>> b='append'
>>> c=0.000102301230102
>>> d=0.1242344213
>>> print('Completed {0}/{1} in {2:<15} seconds: {3:<15} ops/sec'.format(a, b, c, d))
Completed deque/append in 0.000102301230102 seconds: 0.1242344213 ops/sec
You can use f-strings with format spec. 您可以将f字符串用于格式规范。 Basically It's just like str.format. 基本上就像str.format一样。 For example, you can use 例如,您可以使用
a = 2
f'length:{a:<5}cm'
and it will output 它会输出
length:2 cm
but the point is you can set the alignment length programmatically like this 但关键是您可以像这样以编程方式设置对齐长度
a = 2
f'length:{a:<{a+3}}cm'
and I get result 我得到结果
length:2 cm
f-string is new in Python 3.6 and I'm using Python 3.7.1 f-string是Python 3.6中的新增功能,我正在使用Python 3.7.1
https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.