Skip to content

Instantly share code, notes, and snippets.

@mapio
Created July 14, 2011 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapio/1082301 to your computer and use it in GitHub Desktop.
Save mapio/1082301 to your computer and use it in GitHub Desktop.
Trasposizione tabella LaTeX
# -*- coding: utf-8 -*-
# salva questo in un file "pippo.py" ed eseguilo da riga di comando con "python pippo.py"
# metti la tua tabella tra le triple virgolette
table = r"""
a & b & c & d \\
e & f & g & h \\
i & j & k & l
"""
# matrix contiene gli elementi della tabella in una matrice (lista di liste)
matrix = [ map( lambda _ : _.strip(), line.strip( r'\\').split( '&' ) ) for line in table.splitlines() if line ]
# transposed_matrix è la sua trasposta
transposed_matrix = zip( *matrix )
# transposed_table è la tabella trasposta in LaTex
transposed_table = ' \\\\\n'.join( ' & '.join( _ ) for _ in transposed_matrix )
# taglia e incolla il risultato
print transposed_table
@mapio
Copy link
Author

mapio commented Jul 14, 2011

Se lo lanci, produce

a & e & i
b & f & j
c & g & k
d & h & l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment