首页 > 解决方案 > 将两个列表加入到 2 个元素的元组中

问题描述

我有以下数组:

info = ['test', 'apple', 'cars', 'dog']
data = ['cat', 'list', 'text','code']

我想得到以下结果。

test   cat
apple  list
cars   text
dog    code

我用以下代码做到了这一点:

for x, y in zip(info, data):
    print('{0:18} {1:}'.format(x,y))

它有效,但也许有比我更好的选择/方式?

标签: python

解决方案


有很多包可以以表格形式打印内容。我建议上去tabulate

from tabulate import tabulate
t = tabulate(zip(info, data), tablefmt="plain")
print(t)

输出

test   cat
apple  list
cars   text
dog    code

推荐阅读