-
-
Save Sitwon/4485ed8fc6e7b3597f76c0ad511e9d0f to your computer and use it in GitHub Desktop.
Several different ways to format a table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from itertools import starmap | |
def format_table(table, delim='|'): | |
""" | |
:param list table: 2D list, with inner lists of strings as rows. | |
:param str delim: Used to define table cell borders. | |
:return list: Containing formatted string elements representing each row, | |
ready to be written to a file. | |
""" | |
assert len(set(len(row) for row in table)) == 1, "'table' param has rows of differing lengths" | |
col_widths = [len(max(col, key=len)) for col in zip(*table)] | |
left_justify = lambda cell, width: '{{:<{}}}'.format(width).format(cell) | |
justify_cells = lambda cells: starmap(left_justify, zip(cells, col_widths)) | |
make_row = lambda cells: ' {} '.format(delim).join(justify_cells(cells)) | |
make_line = lambda cells: '{0} {1} {0}\n'.format(delim, make_row(cells)) | |
return [make_line(row) for row in table] | |
def format_table_with_map(table, delim='|'): | |
col_widths = [len(max(col, key=len)) for col in zip(*table)] | |
rows_with_widths = map(lambda row: zip(row, col_widths), table) | |
justified_cells = map( | |
lambda row: starmap(lambda cell, width: '{{:<{}}}'.format(width).format(cell), row), | |
rows_with_widths) | |
joined_rows = map(lambda row: ' {} '.format(delim).join(row), justified_cells) | |
return list(map(lambda row: '{0} {1} {0}\n'.format(delim, row), joined_rows)) | |
def format_table_list_comp(table, delim='|'): | |
col_widths = [len(max(col, key=len)) for col in zip(*table)] | |
padded_delim = ' {} '.format(delim) | |
left_justify = lambda cell, width: '{{:<{}}}'.format(width).format(cell) | |
return [ | |
'{0} {1} {0}\n'.format( | |
delim, | |
padded_delim.join([left_justify(cell, width) for cell, width in zip(row, col_widths)])) | |
for row in table | |
] | |
def format_table_for_loops(table, delim='|'): | |
''' Submitted by ircubic ''' | |
def _pad(cell, width): | |
return ' ' + cell + (width - (len(cell) - 1)) * ' ' | |
widths = [0]*len(table[0]) | |
for row in table: | |
for i,col in enumerate(row): | |
col_len = len(str(col)) | |
if col_len > widths[i]: | |
widths[i] = col_len | |
formatted_table = [] | |
for row in table: | |
row_strs = [] | |
for width,col in zip(widths,row): | |
row_strs.append(_pad(str(col), width)) | |
formatted_table.append(delim + delim.join(row_strs) + delim + '\n') | |
return formatted_table | |
def format_table_clever(table, delim='|'): | |
''' Submitted by Corey Cossentino ''' | |
col_widths = [max([len(j) for j in i]) for i in zip(*table)] | |
fmt_string = delim.join([" {{:{}}} ".format(i) for i in col_widths]) | |
return ['{0}{1}{0}\n'.format(delim, fmt_string.format(*row)) for row in table] | |
def format_table_short(table, delim='|'): | |
''' Submitted by Daeken ''' | |
sizes = [max(len(row[i]) for row in table) for i in xrange(len(table[0]))] | |
return ['{0} {1} {0}\n'.format(delim, ' {} '.format(delim).join(cell + ' ' * (sizes[i] - len(cell)) for i, cell in enumerate(row))) for row in table] | |
input_table = [ | |
['abc', 'def', 'ghi', 'jkl'], | |
['1234567890', 'a', 'b', 'c'], | |
['w', 'xx', 'yyy', 'zzzz'], | |
] | |
def print_table(func, table): | |
for line in func(table, delim='|'): | |
print(line, end='') | |
print() | |
print("format_table") | |
print_table(format_table, input_table) | |
print("format_table_with_map") | |
print_table(format_table_with_map, input_table) | |
print("format_table_list_comp") | |
print_table(format_table_list_comp, input_table) | |
print("format_table_for_loops") | |
print_table(format_table_for_loops, input_table) | |
print("format_table_clever") | |
print_table(format_table_clever, input_table) | |
print("format_table_short") | |
print_table(format_table_short, input_table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import starmap | |
def format_table(table, delim='|'): | |
""" | |
:param list table: 2D list, with inner lists of strings as rows. | |
:param str delim: Used to define table cell borders. | |
:return list: Containing formatted string elements representing each row, | |
ready to be written to a file. | |
""" | |
assert len(set(len(row) for row in table)) == 1, "'table' param has rows of differing lengths" | |
col_widths = [len(max(col, key=len)) for col in zip(*table)] | |
left_justify = (cell, width) -> ' {{:<{}}} '.format(width).format(cell) | |
justify_cells = (cells) -> starmap(left_justify, zip(cells, col_widths)) | |
make_row = (cells) -> delim.join(justify_cells(cells)) | |
make_line = (cells) -> '{0}{1}{0}\n'.format(delim, make_row(cells)) | |
return [make_line(row) for row in table] | |
def format_table_coconut(table, delim='|'): | |
col_widths = [col |> map$(len) |> max for col in zip(*table)] | |
left_justify = (cell, width) -> ' {{:<{}}} '.format(width).format(cell) | |
justify_cells = (cells) -> zip(cells, col_widths) |> starmap$(left_justify) | |
make_row = (cells) -> cells |> delim.join | |
make_line = (row) -> '{0}{1}{0}\n'.format(delim, row) | |
return [row |> justify_cells |> make_row |> make_line for row in table] | |
def format_table_with_map(table, delim='|'): | |
col_widths = [col |> map$(len) |> max for col in zip(*table)] | |
return (table | |
|> map$((row) -> zip(row, col_widths)) | |
|> map$((cell_and_width) -> cell_and_width |> starmap$( | |
(cell, width) -> ' {{:<{}}} '.format(width).format(cell))) | |
|> map$((formatted_cells) -> formatted_cells |> delim.join) | |
|> map$((row) -> '{0}{1}{0}\n'.format(delim, row))) | |
def format_table_pipeline(table, delim='|'): | |
row_format_str = (table | |
|*> zip | |
|> map$((col) -> col | |
|> map$(len) | |
|> max | |
|> ' {{:<{}}} '.format) | |
|> delim.join | |
|> '{0}{1}{0}\n'.format$(delim)) | |
return [row |*> row_format_str.format for row in table] | |
def format_table_for_loops(table, delim='|'): | |
''' Submitted by ircubic ''' | |
def _pad(cell, width): | |
return ' ' + cell + (width - (len(cell) - 1)) * ' ' | |
widths = [0]*len(table[0]) | |
for row in table: | |
for i,col in enumerate(row): | |
col_len = len(str(col)) | |
if col_len > widths[i]: | |
widths[i] = col_len | |
formatted_table = [] | |
for row in table: | |
row_strs = [] | |
for width,col in zip(widths,row): | |
row_strs.append(_pad(str(col), width)) | |
formatted_table.append(delim + delim.join(row_strs) + delim + '\n') | |
return formatted_table | |
def format_table_clever(table, delim='|'): | |
''' Submitted by Corey Cossentino ''' | |
col_widths = [max([len(j) for j in i]) for i in zip(*table)] | |
fmt_string = delim.join([" {{:{}}} ".format(i) for i in col_widths]) | |
return ['{0}{1}{0}\n'.format(delim, fmt_string.format(*row)) for row in table] | |
def format_table_short(table, delim='|'): | |
''' Submitted by Daeken ''' | |
sizes = [col |> map$(len) |> max for col in zip(*table)] | |
return ['{0} {1} {0}\n'.format(delim, ' {} '.format(delim).join(cell + ' ' * (sizes[i] - len(cell)) for i, cell in enumerate(row))) for row in table] | |
input_table = [ | |
['abc', 'def', 'ghi', 'jkl'], | |
['1234567890', 'a', 'b', 'c'], | |
['w', 'xx', 'yyy', 'zzzz'], | |
] | |
def print_table(func, table): | |
for line in func(table, delim='|'): | |
line |> print$(end='') | |
print() | |
print("format_table") | |
print_table(format_table, input_table) | |
print("format_table_coconut") | |
print_table(format_table_coconut, input_table) | |
print("format_table_with_map") | |
print_table(format_table_with_map, input_table) | |
print("format_table_pipeline") | |
print_table(format_table_pipeline, input_table) | |
print("format_table_for_loops") | |
print_table(format_table_for_loops, input_table) | |
print("format_table_clever") | |
print_table(format_table_clever, input_table) | |
print("format_table_short") | |
print_table(format_table_short, input_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment