[英]Python - write list list of lists in columns
I have a list of lists with a various number of elements (int). 我有一个列表列表,其中包含不同数量的元素(int)。 I want to print/write it, but in columns rather than in rows.
我想打印/写它,但是在列而不是行中。
Example: 例:
l = [[1,2,3],[4,5],[6,7,8,9],[0]]
Result: 结果:
1 4 6 0
2 5 7 .
3 . 8 .
. . 9 .
The easiest way to do this is to use itertools.izip_longest()
: 最简单的方法是使用
itertools.izip_longest()
:
for x in itertools.izip_longest(*l, fillvalue="."):
print " ".join(str(i) for i in x)
This: 这个:
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)
produces: 生产:
1 4 6 0
2 5 7 .
3 . 8 .
. . 9 .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.