Ask Your Question

Revision history [back]

I think you should continue to manipulate m.str(), but using regular expressions, since m.str() already has the correct spacing. The following will work if you know that the matrix entries will be numbers (plus a possible decimal point and/or minus sign), no letters:

m = matrix([[2222,333,44444], [1,0,0]])
no_brackets =  m.str().replace('[', '').replace(']', '') # replace [, ]
add_commas = re.sub("([-.0-9]+) ", "\\1, ", no_brackets)
print(add_commas)

Output:

 2222,   333, 44444
    1,     0,     0

I think you should continue to manipulate m.str(), but using regular expressions, since m.str() already has the correct spacing. The following will work if you know that the matrix entries will be numbers (plus a possible decimal point and/or minus sign), no letters:

import re
m = matrix([[2222,333,44444], [1,0,0]])
no_brackets =  m.str().replace('[', '').replace(']', '') # replace [, ]
add_commas = re.sub("([-.0-9]+) ", "\\1, ", no_brackets)
print(add_commas)

Output:

 2222,   333, 44444
    1,     0,     0