我试图将嵌套列表显示为列。所以我正在处理的数据是:
tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
我想显示为
^{pr2}$以便条目右对齐。我已经看过Create nice column output in python,但是我不能实现类似的结果。我目前掌握的代码是:
tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
total_len= [[] for x in range(len(tableData))]
longest_string = []
for y1 in range(0, len(tableData)):
for y2 in range(0, len(tableData[y1])):
total_len[y1].append(len(tableData[y1][y2]))
for y1 in range(0, len(total_len)):
longest_string.append(max(total_len[y1]))
for y1 in range(len(tableData)):
for y2 in range(len(tableData[y1])):
print("".join(tableData[y1][y2].rjust(longest_string[y1])))
没有第三方熊猫的类似格式:
输出:
^{pr2}$编辑
以下是一个可以动态显示任意数量的行和列的版本:
来自链接线程的^{} 和solution:
虽然我更喜欢"pandas" dataframe based solution。在
我不知道你有一个^{} 但如果你有,那就很容易了。您可以创建dataframe,然后使用^{} 方法:
相关问题 更多 >
编程相关推荐