在python中以表格格式打印列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17279059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print list in table format in python
提问by Blaise Delaney
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.
我正在从 .txt 文件读取数据,在代码的末尾,我有 5 个列表,我想将它们打印为分隔但空格的列。
采纳答案by arshajii
I'll show you a 3-list analog:
我将向您展示一个 3 列表模拟:
>>> 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
回答by enpenax
Assming that you have a lists of lists:
Assming 你有一个列表列表:
for L in list_of_lists:
print " ".join(L)
The str.join(iterable)function, joins the components of an iterable by the string given.
该str.join(iterable)函数通过给定的字符串连接可迭代的组件。
Therefore, " ".join([1, 2, 3])becomes "1 2 3".
因此," ".join([1, 2, 3])变为“1 2 3”。
In case I might have misunderstood the question and each listis supposed to be a column:
如果我可能误解了这个问题,并且每个问题都list应该是一列:
for T in zip(list1, list2, list3, list4, list5):
print " ".join(T)
zip()merges the given lists to one list of tuples:
zip()将给定的列表合并为一个元组列表:
>>> zip([1,2,3], [4,5,6], [7,8,9])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Cheers!
干杯!
回答by Samuele Mattiuzzo
for nested_list in big_container_list
print '\t'.join(nested_list)
with \tbeing the tabulation character
与\t作为制表字符
quick example:
快速示例:
In [1]: a = [['1','2'],['3','4']]
In [5]: for nested_list in a:
...: print '\t'.join(nested_list)
...:
1 2
3 4
回答by Priyam Singh
You can use my package beautifultable. It supports adding data by rows or columns or even mixing both the approaches. You can insert, remove, update any row or column.
你可以使用我的包beautifultable。它支持按行或列添加数据,甚至可以混合使用这两种方法。您可以插入、删除、更新任何行或列。
Usage
用法
>>> from beautifultable import BeautifulTable
>>> table = BeautifulTable()
>>> table.column_headers = ["name", "rank", "gender"]
>>> table.append_row(["Jacob", 1, "boy"])
>>> table.append_row(["Isabella", 1, "girl"])
>>> table.append_row(["Ethan", 2, "boy"])
>>> table.append_row(["Sophia", 2, "girl"])
>>> table.append_row(["Michael", 3, "boy"])
>>> print(table)
+----------+------+--------+
| name | rank | gender |
+----------+------+--------+
| Jacob | 1 | boy |
+----------+------+--------+
| Isabella | 1 | girl |
+----------+------+--------+
| Ethan | 2 | boy |
+----------+------+--------+
| Sophia | 2 | girl |
+----------+------+--------+
| Michael | 3 | boy |
+----------+------+--------+
Have fun
玩得开心
回答by BeastCoder
Try out my library
试试我的图书馆
NOTE: This answer is already posted on this question, and this question.
I just made a library for this that I think could really help. It is extremely simple, that's why I think you should use it. It is called TableIT.
我刚刚为此创建了一个库,我认为这真的很有帮助。它非常简单,这就是为什么我认为你应该使用它。它被称为TableIT。
Basic Use
基本用途
To use it, first follow the download instructions on the GitHub Page.
要使用它,请首先按照GitHub 页面上的下载说明进行操作。
Then import it:
然后导入:
import TableIt
Then make a list of lists where each inner list is a row:
然后制作一个列表列表,其中每个内部列表都是一行:
table = [
[4, 3, "Hi"],
[2, 1, 808890312093],
[5, "Hi", "Bye"]
]
Then all you have to do is print it:
然后你所要做的就是打印它:
TableIt.printTable(table)
This is the output you get:
这是你得到的输出:
+--------------------------------------------+
| 4 | 3 | Hi |
| 2 | 1 | 808890312093 |
| 5 | Hi | Bye |
+--------------------------------------------+
Field Names
字段名称
You can use field names if you want to (if you aren't using field names you don't have to say useFieldNames=False because it is set to that by default):
如果您愿意,您可以使用字段名称(如果您不使用字段名称,则不必说 useFieldNames=False 因为它默认设置为该名称):
TableIt.printTable(table, useFieldNames=True)
From that you will get:
从中你会得到:
+--------------------------------------------+
| 4 | 3 | Hi |
+--------------+--------------+--------------+
| 2 | 1 | 808890312093 |
| 5 | Hi | Bye |
+--------------------------------------------+
There are other uses to, for example you could do this:
还有其他用途,例如您可以这样做:
import TableIt
myList = [
["Name", "Email"],
["Richard", "[email protected]"],
["Tasha", "[email protected]"]
]
TableIt.print(myList, useFieldNames=True)
From that:
从中:
+-----------------------------------------------+
| Name | Email |
+-----------------------+-----------------------+
| Richard | [email protected] |
| Tasha | [email protected] |
+-----------------------------------------------+
Or you could do:
或者你可以这样做:
import TableIt
myList = [
["", "a", "b"],
["x", "a + x", "a + b"],
["z", "a + z", "z + b"]
]
TableIt.printTable(myList, useFieldNames=True)
And from that you get:
从中你得到:
+-----------------------+
| | a | b |
+-------+-------+-------+
| x | a + x | a + b |
| z | a + z | z + b |
+-----------------------+
Colors
颜色
You can also use colors.
您也可以使用颜色。
You use colors by using the color option (by default it is set to None) and specifying RGB values.
您可以通过使用 color 选项(默认设置为 None)并指定 RGB 值来使用颜色。
Using the example from above:
使用上面的例子:
import TableIt
myList = [
["", "a", "b"],
["x", "a + x", "a + b"],
["z", "a + z", "z + b"]
]
TableIt.printTable(myList, useFieldNames=True, color=(26, 156, 171))
Then you will get:
然后你会得到:
Please note that printing colors might not work for you but it does works the exact same as the other libraries that print colored text. I have tested and every single color works. The blue is not messed up either as it would if using the default 34mANSI escape sequence (if you don't know what that is it doesn't matter). Anyway, it all comes from the fact that every color is RGB value rather than a system default.
请注意,打印颜色可能不适合您,但它的工作原理与打印彩色文本的其他库完全相同。我已经测试过,每种颜色都有效。蓝色也不会像使用默认的34mANSI 转义序列那样混乱(如果您不知道那是什么也没关系)。无论如何,这一切都来自这样一个事实,即每种颜色都是 RGB 值,而不是系统默认值。
More Info
更多信息
For more info check the GitHub Page
有关更多信息,请查看GitHub 页面


