1)
li = ['sea','mountain','desert',
'Emma','Cathy','Kate',
'ii','uuuuuuuuuuuuuuuuuuu','aaa',
'round','flat','sharp',
'blueberry','banana','apple',
'red','purple','white',
'hen','tiger']
a,b = divmod(len(li),3)
itn = iter(li).next
print ''.join('%s\t%s\t%s\n' % (itn(),itn(),itn())
for i in xrange(a))\
+ ('%s\t%s\t\n' % (itn(),itn()) if b==2
else '%s\t\n' % itn() if b==1
else '')
结果
sea mountain desert
Emma Cathy Kate
ii uuuuuuuuuuuuuuuuuuu aaa
round flat sharp
blueberry banana apple
red purple white
hen tiger
.
2)
并在宽度取决于列表中最长元素的列中对齐:
li = ['sea','mountain','desert',
'Emma','Cathy','Kate',
'HH','VVVVVVV','AAA',
'round','flat','sharp',
'blueberry','banana','apple',
'red','purple','white',
'hen','tiger']
maxel = max(len(el) for el in li)
a,b = divmod(len(li),3)
itn = iter(li).next
form = '%%-%ds\t%%-%ds\t%%-%ds\n' % (maxel,maxel,maxel)
print ''.join(form % (itn(),itn(),itn())
for i in xrange(a))\
+ ('%%-%ds\t%%-%ds\t\n' %(maxel,maxel) % (itn(),itn()) if b==2
else '%%-%ds\t\n' % ma% itn() if b==1
else '')
结果
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
blueberry banana apple
red purple white
hen tiger
.
3)
要在列中对齐,每列的宽度取决于其中最长的元素:
li = ['sea','mountain','desert',
'Emma','Cathy','Kate',
'HH','VVVVVVV','AAA',
'round','flat','sharp',
'nut','banana','apple',
'red','purple','white',
'hen','tiger']
maxel0 = max(len(li[i]) for i in xrange(0,len(li),3))
maxel1 = max(len(li[i]) for i in xrange(1,len(li),3))
maxel2 = max(len(li[i]) for i in xrange(2,len(li),3))
a,b = divmod(len(li),3)
itn = iter(li).next
form = '%%-%ds\t%%-%ds\t%%-%ds\n' % (maxel0,maxel1,maxel2)
print ''.join(form % (itn(),itn(),itn())
for i in xrange(a))\
+ ('%%-%ds\t%%-%ds\t\n' %(maxel0,maxel1) % (itn(),itn()) if b==2
else '%%-%ds\t\n' % maxel0 % itn() if b==1
else '')
结果
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
nut banana apple
red purple white
hen tiger
4)
我已经修改了算法,以便推广到想要的任意数量的列。
所需的列数必须作为参数传递给 parameter nc
:
from itertools import imap,islice
li = ['sea','mountain','desert',
'Emma','Cathy','Kate',
'HH','VVVVVVV','AAA',
'round','flat','sharp',
'nut','banana','apple',
'heeeeeeeeeeen','tiger','snake'
'red','purple','white',
'atlantic','pacific','antarctic',
'Bellini']
print 'len of li == %d\n' % len(li)
def cols_print(li,nc):
maxel = tuple(max(imap(len,islice(li,st,None,nc)))
for st in xrange(nc))
nblines,tail = divmod(len(li),nc)
stakes = (nc-1)*['%%-%ds\t'] + ['%%-%ds']
form = ''.join(stakes) % maxel
itn = iter(li).next
print '\n'.join(form % tuple(itn() for g in xrange(nc))
for i in xrange(nblines))
if tail:
print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
else:
print
for nc in xrange(3,8):
cols_print(li,nc)
print '-----------------------------------------------------------'
结果
len of li == 24
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
nut banana apple
heeeeeeeeeeen tiger snakered
purple white atlantic
pacific antarctic Bellini
-----------------------------------------------------------
sea mountain desert Emma
Cathy Kate HH VVVVVVV
AAA round flat sharp
nut banana apple heeeeeeeeeeen
tiger snakered purple white
atlantic pacific antarctic Bellini
-----------------------------------------------------------
sea mountain desert Emma Cathy
Kate HH VVVVVVV AAA round
flat sharp nut banana apple
heeeeeeeeeeen tiger snakered purple white
atlantic pacific antarctic Bellini
-----------------------------------------------------------
sea mountain desert Emma Cathy Kate
HH VVVVVVV AAA round flat sharp
nut banana apple heeeeeeeeeeen tiger snakered
purple white atlantic pacific antarctic Bellini
-----------------------------------------------------------
sea mountain desert Emma Cathy Kate HH
VVVVVVV AAA round flat sharp nut banana
apple heeeeeeeeeeen tiger snakered purple white atlantic
pacific antarctic Bellini
-----------------------------------------------------------
.
但我更喜欢列之间没有制表符的显示,但只有给定数量的字符。
在下面的代码中,我选择用 2 个字符分隔列:它是行中的 2
maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2
编码
from itertools import imap,islice
li = ['sea','mountain','desert',
'Emma','Cathy','Kate',
'HH','VVVVVVV','AAA',
'round','flat','sharp',
'nut','banana','apple',
'heeeeeeeeeeen','tiger','snake'
'red','purple','white',
'atlantic','pacific','antarctic',
'Bellini']
print 'len of li == %d\n' % len(li)
def cols_print(li,nc):
maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2
for st in xrange(nc))
nblines,tail = divmod(len(li),nc)
stakes = nc*['%%-%ds']
form = ''.join(stakes) % maxel
itn = iter(li).next
print '\n'.join(form % tuple(itn() for g in xrange(nc))
for i in xrange(nblines))
if tail:
print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
else:
print
for nc in xrange(3,8):
cols_print(li,nc)
print 'mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm'
结果
len of li == 24
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
nut banana apple
heeeeeeeeeeen tiger snakered
purple white atlantic
pacific antarctic Bellini
mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea mountain desert Emma
Cathy Kate HH VVVVVVV
AAA round flat sharp
nut banana apple heeeeeeeeeeen
tiger snakered purple white
atlantic pacific antarctic Bellini
mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea mountain desert Emma Cathy
Kate HH VVVVVVV AAA round
flat sharp nut banana apple
heeeeeeeeeeen tiger snakered purple white
atlantic pacific antarctic Bellini
mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea mountain desert Emma Cathy Kate
HH VVVVVVV AAA round flat sharp
nut banana apple heeeeeeeeeeen tiger snakered
purple white atlantic pacific antarctic Bellini
mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea mountain desert Emma Cathy Kate HH
VVVVVVV AAA round flat sharp nut banana
apple heeeeeeeeeeen tiger snakered purple white atlantic
pacific antarctic Bellini
mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm