How to store and print a list of numbers in matrix form(python) -
i have list of numbers output data of ocr operation. there 40 intergers, want print them in form of matrix(8x5). can 1 please me how in python 2.7? dont want enter elements manually.. list of elements being generated using loops, want display them in form of 8x5 matrix.
thank you
simply use list comprehension , range()
function.
my_list = [1, 2, 3, ..., 40] array = [[my_list[j*5 + i] in range(5)] j in range(8)]
you can use function display matrix:
for row in array: print(row)
if need matrix "nicely" displayed, can use happyleapsecond's solution:
print('\n'.join([''.join(['{:4}'.format(item) item in row]) row in array]))
see example: https://ideone.com/yok1i5
Comments
Post a Comment