Okay this is probably a really dumb question, however it’s really starting to hurt. I have a numpy matrix, and basically I print it out row by row. However I want to make each row be formatted and separated properly.
>>> arr = numpy.matrix([[x for x in range(5)] for y in range(5)])
>>> arr
matrix([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
Lets say i want to print the first row, and add a ‘|’ between each element:
>>> '|'.join(map(str, arr[0,]))
'[[0 1 2 3 4]]'
Err…
>>> '|'.join(map(lambda x: str(x[0]), arr[0]))
'[[0 1 2 3 4]]'
I am really confused by this behavior why does it do this?
arris returned as amatrixtype, which may not be an iterable object that plays nicely withjoin.You could convert
arrto alistwithtolist()and then perform yourjoin.Or with an array using
numpy.asarryfor that matter