【问题标题】:List of lists into a Python Rich tablePython Rich 表中的列表列表
【发布时间】:2020-12-09 12:32:06
【问题描述】:

鉴于以下情况,请问如何将动物、年龄和性别放入每个表格单元格中?目前,所有数据最终都在一个单元格中。谢谢

from rich.console import Console
from rich.table import Table

list = [['Cat', '7', 'Female'],
        ['Dog', '0.5', 'Male'],
        ['Guinea Pig', '5', 'Male']]

table1 = Table(show_header=True, header_style='bold')
table1.add_column('Animal')
table1.add_column('Age')
table1.add_column('Gender')

for row in zip(*list):
    table1.add_row(' '.join(row))

console.print(table1)

【问题讨论】:

  • 富人的定义在哪里/是什么? Table?是外包装吗?
  • 抱歉我的错误 - 错过了导入。

标签: python rich


【解决方案1】:

只需使用* 解包元组,它应该可以正常工作。

for row in zip(*list):
    table1.add_row(*row)

注意

table1.add_row(*('Cat', 'Dog', 'Guinea Pig'))

等价于

table1.add_row('Cat', 'Dog', 'Guinea Pig')

虽然以前您的方法等同于

table1.add_row('Cat Dog Guinea Pig')

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2014-12-18
    • 2012-07-14
    相关资源
    最近更新 更多