Skip to content

Instantly share code, notes, and snippets.

@kaspermunch
Last active September 1, 2023 08:19
Show Gist options
  • Save kaspermunch/a067930e86a48ab08e8eb082796525ec to your computer and use it in GitHub Desktop.
Save kaspermunch/a067930e86a48ab08e8eb082796525ec to your computer and use it in GitHub Desktop.
List gene names nicely in jupyter by printing in columns
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