This code is essentially the scoffey implementation above, but it does not have three character restrictions and is slightly more powerful. Here is my code:
def format__1(digits,num): if digits<len(str(num)): raise Exception("digits<len(str(num))") return ' '*(digits-len(str(num))) + str(num) def printmat(arr,row_labels=[], col_labels=[]): #print a 2d numpy array (maybe) or nested list max_chars = max([len(str(item)) for item in flattenList(arr)+col_labels]) #the maximum number of chars required to display any item in list if row_labels==[] and col_labels==[]: for row in arr: print '[%s]' %(' '.join(format__1(max_chars,i) for i in row)) elif row_labels!=[] and col_labels!=[]: rw = max([len(str(item)) for item in row_labels]) #max char width of row__labels print '%s %s' % (' '*(rw+1), ' '.join(format__1(max_chars,i) for i in col_labels)) for row_label, row in zip(row_labels, arr): print '%s [%s]' % (format__1(rw,row_label), ' '.join(format__1(max_chars,i) for i in row)) else: raise Exception("This case is not implemented...either both row_labels and col_labels must be given or neither.")
working
import numpy x = numpy.array([[85, 86, 87, 88, 89], [90, 191, 192, 93, 94], [95, 96, 97, 98, 99], [100,101,102,103,104]]) row_labels = ['Z', 'Y', 'X', 'W'] column_labels = ['A', 'B', 'C', 'D', 'E'] printmat(x,row_labels=row_labels, col_labels=column_labels)
gives
ABCDE Z [ 85 86 87 88 89] Y [ 90 191 192 93 94] X [ 95 96 97 98 99] W [100 101 102 103 104]
This will also be the output if "x" was just a nested python list instead of a numpy array.