如何在Python中打印长度不同的列表的表格

3 投票
1 回答
2068 浏览
提问于 2025-04-16 22:09

我该如何从两个长度不同的列表中打印出一个表格(每个列表作为一列)呢?

举个例子:

>>> l1=['Cat', 'Dog', 'Gorilla', 'Ladybug']
>>> l2=['Cat', 'Dog']
>>> print_chart(l1, l2)
Cat        Cat
Dog        Dog
Gorilla
Ladybug

使用 rjust 可能会很有帮助。

1 个回答

6

使用 itertools.izip_longest

for a, b in izip_longest(l1, l2, fillvalue=''):
    print "{0:20s}\t{1:20s}".format(a, b)

撰写回答