【问题标题】:Tabulate module in Python not giving the desired outputPython中的制表模块未提供所需的输出
【发布时间】:2020-05-15 01:00:32
【问题描述】:

我在 Python 中使用制表符来获取列表:

import tabulate import tabulate
List4 = ['xyz-34t',  'abc-237', 'abc-eq1']
print tabulate(List4)

预期输出是

xyz-34t
abc-237
abc-eq1

实际输出:

x y z - 3 4 t
a b c - 2 3 7
a b c - e q 1

【问题讨论】:

  • 尝试在 List4 周围放置一组额外的括号
  • @m02ph3u5 不起作用

标签: python tabulate


【解决方案1】:

按照https://pypi.org/project/tabulate/ 的描述,你想要的可能是:

import tabulate

list4 = [['xyz-34t'], ['abc-237'], ['abc-eq1']]
print tabulate(list4)

回复您的评论:如果您的意思是上面的列表是output of another function(您现在可以控制),因此如果您要问如何带来这个

['xyz-34t', 'abc-237', 'abc-eq1']

到这里

[['xyz-34t'], ['abc-237'], ['abc-eq1']]

答案是(使用列表理解):

list = ['xyz-34t', 'abc-237', 'abc-eq1']
list_of_lists = [[x] for x in list]

【讨论】:

  • 列表我用作某些函数的输出,那么如何将列表转换为上述格式?
猜你喜欢
  • 2020-11-30
  • 2023-03-12
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2022-01-24
  • 2016-09-19
  • 2021-06-05
  • 1970-01-01
相关资源
最近更新 更多