问题描述
我知道我以前曾经在某个地方看到过一些例子,但是对于我一生来说,在谷歌搜索时找不到它.
I know that I've seen some example somewhere before but for the life of me I cannot find it when googling around.
我有一些数据行:
data = [[1,2,3],
[4,5,6],
[7,8,9],
]
我想将此数据输出到表中,例如
And I want to output this data in a table, e.g.
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
很显然,我可以使用诸如prettytable之类的库,或者下载熊猫之类的东西,但是我对此并不感兴趣.
Obviously I could use a library like prettytable or download pandas or something but I'm very disinterested in doing that.
我只想在Jupyter笔记本单元中将行输出为表格.我该怎么做?
I just want to output my rows as tables in my Jupyter notebook cell. How do I do this?
推荐答案
我终于重新找到了 jupyter/IPython文档.
我需要这个:
from IPython.display import HTML, display
data = [[1,2,3],
[4,5,6],
[7,8,9],
]
display(HTML(
'<table><tr>{}</tr></table>'.format(
'</tr><tr>'.join(
'<td>{}</td>'.format('</td><td>'.join(str(_) for _ in row)) for row in data)
)
))
(我可能对理解的理解有些微,但我们需要的是display(HTML('some html here'))
)
(I may have slightly mucked up the comprehensions, but display(HTML('some html here'))
is what we needed)
这篇关于如何在Jupyter Notebook中将列表输出为表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!