【发布时间】:2017-03-16 00:27:38
【问题描述】:
我正在尝试创建一个函数,该函数输出一个矩阵,该矩阵在单独的行上包含列表中的每个项目,中间有行。我得到的唯一输出是引号('')。我不明白为什么。我想我已经正确设置了它以输出所需的内容,但必须缺少一些东西?
我在代码下方包含了示例。
def show_table(table):
table=[]
s=[[str(e) for e in row] for row in table]
lens= [max(map(len, col)) for col in zip(*s)]
fmt= '\t'.join('{{:{}}}'.format(x) for x in lens)
table= [fmt.format(*row) for row in s]
return '\n'.join(table)
show_table([['A','BB'],['C','DD']])
输出:
'|一个 | BB |\n| C | DD |\n'
print(show_table([['A','BB'],['C','DD']])) 输出:
|一个 | BB | | C | DD |
【问题讨论】:
标签: function python-3.x join matrix format