将输出格式化为表格 [英] Formatting output as table

查看:27
本文介绍了将输出格式化为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例输入:

[('b', 'c', 4),('l', 'r', 5),('i', 'a', 6),('c', 't', 7),('a', '$', 8),('n', '$', 9)]

[0] 包含垂直标题,[1] 包含水平标题.

示例输出:

 cr a t $ $乙 4升 5我 67一个 89

注意:给定足够多的元组,可以填充整个表格:P

如何使用 [最好] 一行代码在 Python 中将输出格式化为表格?

解决方案

这是您修改后的问题的答案:

data = [['A','a','1'],['B','b','2'],['C','c','3'],['D','d','4']]# 期望输出:##   A B C D# 1# b 2# c 3# d 4# 检查数据由 colname, rowname, value 三元组组成assert all([3 == len(row) for row in data])# 将所有数据转换为字符串数据 = [ [str(c) for c in r] for r in data]# 检查所有数据是否为一个字符宽assert all([1 == len(s) for s in r for r in data])#==============================================================================# 详细版本#==============================================================================col_names, row_names, values = zip(*data) # 转置header_line = ' ' + ' '.join(col_names)row_lines = []对于 idx, (row_name, value) in enumerate(zip(row_names,values)):# 使用 ' '*n 得到 2n 个连续的空格.row_line = row_name + ' ' + ' '*idx + valuerow_lines.append(row_line)打印 header_line对于 row_lines 中的 r:打印 (r)

或者,如果这对你来说太长了,试试这个:

cs, rs, vs = zip(*data)print ('\n'.join([' '+' '.join(cs)] + [r+' '+' '*i+v for i,(r,v) in enumerate(zip(rs,vs))]))

两者都有以下输出:

 A B C D1乙 234

<小时>

这是您想要的核心内容(没有读者行或标题列)

<预><代码>>>>print('\n'.join([ ''.join([str(i+j+2).rjust(3)for i in range(10)]) for j in range(10) ]))2 3 4 5 6 7 8 9 10 113 4 5 6 7 8 9 10 11 124 5 6 7 8 9 10 11 12 135 6 7 8 9 10 11 12 13 146 7 8 9 10 11 12 13 14 157 8 9 10 11 12 13 14 15 168 9 10 11 12 13 14 15 16 179 10 11 12 13 14 15 16 17 1810 11 12 13 14 15 16 17 18 1911 12 13 14 15 16 17 18 19 20

它使用对 ij 的嵌套列表理解来生成数字 i+j,然后 str.rjust() 将所有字段的长度填充为三个字符,最后使用 str.join() 将所有子字符串放在一起.

Example input:

[('b', 'c', 4),
('l', 'r', 5),
('i', 'a', 6),
('c', 't', 7),
('a', '$', 8),
('n', '$', 9)]

[0] contains the vertical heading, [1] contains the horizontal heading.

Example output:

  c r a t $ $
b 4  
l   5
i     6
c       7
a         8
n           9

Note: given enough tuples the entire table could be filled :P

How do I format output as a table in Python using [preferably] one line of code?

解决方案

Here's an answer for your revised question:

data = [
    ['A','a','1'],
    ['B','b','2'],
    ['C','c','3'],
    ['D','d','4']
]

# Desired output:
#
#   A B C D
# a 1
# b   2
# c     3
# d       4

# Check data consists of colname, rowname, value triples
assert all([3 == len(row) for row in data])
# Convert all data to strings
data = [ [str(c) for c in r] for r in data]
# Check all data is one character wide
assert all([1 == len(s) for s in r for r in data])

#============================================================================
# Verbose version
#============================================================================
col_names, row_names, values = zip(*data) # Transpose

header_line = '  ' + ' '.join(col_names)
row_lines = []
for idx, (row_name, value) in enumerate(zip(row_names,values)):
    # Use '  '*n to get 2n consecutive spaces.
    row_line = row_name + ' ' + '  '*idx + value
    row_lines.append(row_line)

print header_line
for r in row_lines:
    print (r)

Or, if that's too long for you, try this:

cs, rs, vs = zip(*data)
print ('\n'.join(['  '+' '.join(cs)] + [r+' '+'  '*i+v for i,(r,v) in enumerate(zip(rs,vs))]))

Both have the following output:

  A B C D
a 1
b   2
c     3
d       4


Here's the kernel of what you want (no reader row or header column)

>>> print('\n'.join([ ''.join([str(i+j+2).rjust(3)
    for i in range(10)]) for j in range(10) ]))

  2  3  4  5  6  7  8  9 10 11
  3  4  5  6  7  8  9 10 11 12
  4  5  6  7  8  9 10 11 12 13
  5  6  7  8  9 10 11 12 13 14
  6  7  8  9 10 11 12 13 14 15
  7  8  9 10 11 12 13 14 15 16
  8  9 10 11 12 13 14 15 16 17
  9 10 11 12 13 14 15 16 17 18
 10 11 12 13 14 15 16 17 18 19
 11 12 13 14 15 16 17 18 19 20

It uses a nested list comprehension over i and j to generate the numbers i+j, then str.rjust() to pad all fields to three characters in length, and finally some str.join()s to put all the substrings together.

这篇关于将输出格式化为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆