printing - print matrix with indicies python -
i have matrix in python defined this:
matrix = [['a']*4 in range(4)]
how print in following format:
0 1 2 3 0 1 2 3
this function matches exact output.
>>> def printmatrix(testmatrix): print ' ', in range(len(testmatrix[1])): # make work non square matrices. print i, print i, element in enumerate(testmatrix): print i, ' '.join(element) >>> matrix = [['a']*4 in range(4)] >>> printmatrix(matrix) 0 1 2 3 0 a a 1 a a 2 a a 3 a a >>> matrix = [['a']*6 in range(4)] >>> printmatrix(matrix) 0 1 2 3 4 5 0 a a a 1 a a a 2 a a a 3 a a a
to check single length elements , put &
in place of elements length > 1, put check in list comprehension, code change follows.
>>> def printmatrix2(testmatrix): print ' ', in range(len(testmatrix[1])): print i, print i, element in enumerate(testmatrix): print i, ' '.join([elem if len(elem) == 1 else '&' elem in element]) >>> matrix = [['a']*6 in range(4)] >>> matrix[1][1] = 'ab' >>> printmatrix(matrix) 0 1 2 3 4 5 0 a a a 1 ab a a 2 a a a 3 a a a >>> printmatrix2(matrix) 0 1 2 3 4 5 0 a a a 1 & a a 2 a a a 3 a a a
Comments
Post a Comment