我有以下程序使用我从网上复制的双端队列。
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))
输出如下所示。
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
我正在寻找一种很好的方法来对齐数字seconds
和ops/second
。这是如何在python中完成的
答案 0 :(得分:2)
如果你需要更多的地方而不仅仅是这个,你也可以使用tabulate模块,它会很好地为你格式化,并且不需要预先猜测长度。
答案 1 :(得分:1)
您需要将Completed deque/append
部分与其他要对齐的列对齐。试试这个:
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
答案 2 :(得分:0)
你可以使用&lt;和&gt; format()字符串方法的对齐运算符。请查看此处的文档:
https://docs.python.org/3.5/library/string.html#format-string-syntax
例如:
>>> 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
答案 3 :(得分:0)
您可以将f-strings用于格式规范。基本上就像str.format一样。例如,您可以使用
a = 2
f'length:{a:<5}cm'
它将输出
length:2 cm
但是关键是您可以像这样通过编程方式设置对齐长度
a = 2
f'length:{a:<{a+3}}cm'
我得到结果
length:2 cm
f-string是Python 3.6中的新功能,我正在使用Python 3.7.1
https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals