Last active
September 1, 2023 08:19
-
-
Save kaspermunch/a067930e86a48ab08e8eb082796525ec to your computer and use it in GitHub Desktop.
List gene names nicely in jupyter by printing in columns
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 math import sqrt | |
from itertools import zip_longest | |
def list_genes(words, ncols=None): | |
n = len(words) | |
col_width = max(map(len, words)) + 1 | |
if ncols is None: | |
ncols = max(100//col_width, 1+sqrt(n/col_width)) | |
nrows = int(n/ncols) + 1 | |
rows = [] | |
for r in range(0, n, nrows): | |
rows.append(words[r:r+nrows]) | |
for row in list(zip_longest(*rows, fillvalue='')): | |
line = [] | |
for gene in row: | |
line.append(gene.ljust(col_width)) | |
print(''.join(line)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment