在python中以表格格式打印列表 [英] Print list in table format in python

查看:50
本文介绍了在python中以表格格式打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个列表(等长)打印为表格的列.

我正在从 .txt 文件中读取数据,在代码的末尾,我有 5 个列表,我想将它们打印为分隔但空格的列.

解决方案

我将向您展示一个 3-list 模拟:

<预><代码>>>>l1 = ['a', 'b', 'c']>>>l2 = ['1', '2', '3']>>>l3 = ['x', 'y', 'z']>>>对于 zip(l1, l2, l3) 中的行:... 打印 ' '.join(row)1 x2 年3 z

I am trying to print several lists (equal length) as columns of an table.

I am reading data from a .txt file, and at the end of the code, I have 5 lists, which I would like to print as columns separated but space.

解决方案

I'll show you a 3-list analog:

>>> l1 = ['a', 'b', 'c']
>>> l2 = ['1', '2', '3']
>>> l3 = ['x', 'y', 'z']
>>> for row in zip(l1, l2, l3):
...     print ' '.join(row)

a 1 x
b 2 y
c 3 z

这篇关于在python中以表格格式打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆