2011-02-05 7 views
6

numaralı kutudaki listelerin listesini listeler. Çeşitli sayıda öğeye sahip bir liste listem var (int). Yazmak/yazmak istiyorum, ancak satırlar yerine sütunlarda.Python -

Örnek:

l = [[1,2,3],[4,5],[6,7,8,9],[0]] 

Sonuç:

1 4 6 0 
2 5 7 . 
3 . 8 . 
. . 9 . 

cevap

14

Bunu yapmanın en kolay yolu kullanmak itertools.izip_longest():

for x in itertools.izip_longest(*l, fillvalue="."): 
    print " ".join(str(i) for i in x) 
3

Bu:

import itertools 

l = [[1,2,3],[4,5],[6,7,8,9],[0]] 

for t in itertools.izip_longest(*l): 
    print "".join("%3d" % x if x is not None else " ." for x in t) 

üretir:

1 4 6 0 
    2 5 7 . 
    3 . 8 . 
    . . 9 .