Kas*_*dra 2 python printing iteration tabs list
我有一个包含107个名字的列表,我想以3个左右的组形式打印出来,每个名称用一个标签分隔,每行后面有一个换行符,直到结束.我怎样才能做到这一点?
与for item in list print item我只能得到每场的行1名,这是很好的我想,但我想更适合在控制台一次,所以我想在每行打印3名左右的名字,因为我去通过列表,而不是:
name1
name2
name3
name4
name5
name6
Run Code Online (Sandbox Code Playgroud)
我会得到:
name1 name2 name3
name4 name5 name6
Run Code Online (Sandbox Code Playgroud)
有点难以找到答案,我无法想出我需要的东西,或者我能理解的东西,我发现的大多数事情只是处理len()或range()混淆了我.有一些简单的方法可以做到这一点吗?谢谢!
[edit:update]使用@ inspectorG4dget的示例:
for i in range(0, len(listnames), 5):
print '\t\t'.join(listnames[i:i+5])
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:http://www.pasteall.org/pic/show.php?id = 41159
我怎样才能清理干净,以便每列中的所有内容都很好地对齐?我想要的容易吗?
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 '')
Run Code Online (Sandbox Code Playgroud)
结果
sea mountain desert
Emma Cathy Kate
ii uuuuuuuuuuuuuuuuuuu aaa
round flat sharp
blueberry banana apple
red purple white
hen tiger
Run Code Online (Sandbox Code Playgroud)
.
并在宽度取决于列表中最长元素的列中对齐:
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 '')
Run Code Online (Sandbox Code Playgroud)
结果
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
blueberry banana apple
red purple white
hen tiger
Run Code Online (Sandbox Code Playgroud)
.
要在列中对齐,每列的宽度取决于其中最长的元素:
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 '')
Run Code Online (Sandbox Code Playgroud)
结果
sea mountain desert
Emma Cathy Kate
HH VVVVVVV AAA
round flat sharp
nut banana apple
red purple white
hen tiger
Run Code Online (Sandbox Code Playgroud)
我已经修改了算法,以便推广到任意数量的列.
所需的列数必须作为参数传递给参数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 '-----------------------------------------------------------'
Run Code Online (Sandbox Code Playgroud)
结果
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
-----------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
.
但我更喜欢一种显示,其中列之间没有选项卡,但只有给定数量的字符.
在下面的代码中,我选择将列分隔2个字符:它是行中的2
maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2
Run Code Online (Sandbox Code Playgroud)
代码
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'
Run Code Online (Sandbox Code Playgroud)
结果
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2505 次 |
| 最近记录: |