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
如果您需要的位置不仅限于此,还可以使用制表模块,该模块将为您很好地设置格式,而无需事先猜测长度。
You need to align Completed deque/append
part as well for the rest of columns to be aligned. 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. Look at the documentation here:
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. Basically It's just like 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
https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.