Skip to content

Instantly share code, notes, and snippets.

@tjs-w
Created September 12, 2022 20:42
Show Gist options
  • Save tjs-w/7afdb19058293a9c0d46047dced85f25 to your computer and use it in GitHub Desktop.
Save tjs-w/7afdb19058293a9c0d46047dced85f25 to your computer and use it in GitHub Desktop.
def tabulate(dlist, width=12):
# good enough, could be sick
fmt = f"{{:^{width}}}|" * len(dlist)
sep = f"{{:^{width}}}+" * len(dlist)
print("+" + sep.format(*["-" * width] * len(dlist)))
print("|" + fmt.format(*[str(k).upper() for k, _ in dlist.items()]))
print("+" + sep.format(*["-" * width] * len(dlist)))
list_of_lists = []
max_rows = 0
for _, v in dlist.items():
list_of_lists.append(v)
if max_rows < len(v):
max_rows = len(v)
for row_idx in range(0, max_rows):
row = []
for col_idx in range(0, len(dlist)):
row.append(
list_of_lists[col_idx][row_idx]
if row_idx < len(list_of_lists[col_idx])
else ""
)
print("|" + fmt.format(*row))
print("+" + sep.format(*["-" * width] * len(dlist)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment