Last active
March 10, 2020 01:42
-
-
Save teebu/f7c77d29ef2b8b8c87d02cfafeb325f2 to your computer and use it in GitHub Desktop.
Print columns and rows of array elements with padding
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
def col_print(title, array, term_width=150, pad_size=1): | |
indent = " " * 4 | |
pad = " " * pad_size | |
title += "\n" | |
if not array: | |
return title + indent + "<None>" | |
array = list(map(str, array)) # make every element a string | |
max_item_width = max(map(len, array)) | |
num_rows = int(math.ceil(len(array) / ((term_width + pad_size) // (max_item_width + pad_size)))) | |
return title + "\n".join(indent + pad.join(item.ljust(max_item_width) for item in array[index::num_rows]) | |
for index in range(num_rows)) | |
# Usage: | |
print(col_print('Numbers:', list(range(1000)), pad_size=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment