On Tuesday, May 17, 2022 at 8:48:27 AM UTC+8, Dennis Lee Bieber wrote: > On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com" > <hongy...@gmail.com> declaimed the following: > > > > > >I tried with the repr() method as follows, but it doesn't give any output: > I have no idea what 50% of those libraries are supposed to do, and am > not going to install them just to try out your posted code. If you really > want such help, post the MINIMUM example code the produces your problem. > > >a=str(strmat(lst)) > >a=re.sub(r"'","",a) > > Explain what you believe this operation is doing, show us the input and > the output. > > The best I can make out of that is that it is looking for single quote > characters within whatever "a" is, and replacing them with nothing. > Something much more understandable, without invoking a regular expression > library (especially when neither the search nor the replacement terms are > regular expressions) with simple string operations... > > stripped = "".join(quoted.split("'"))
Thank you for your above trick. I tried with the following code snippet: ``` from fractions import Fraction def strmat(m): if(np.array([m]).ndim==1): return str(Fraction(m)) else: return list(map(lambda L:strmat(L), np.array(m))) # For test: b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], [0.0, 0.0, 0.0, 1.0]] a=str(strmat(b)) a1=stripped = "".join(a.split("'")) a=re.sub(r"'","",a) #repr(a) print("a1 = "+ a1) print("a = "+ a) ``` As you can see, both methods give the same results: ``` a1 = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]] a = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]] ``` > You also don't need to specify RAW format for the "'" -- Python is quite > happy mixing single and double quotes (that is: single quotes inside a > string using double quotes, double quotes inside a string using single > quotes, either inside strings using triply quoted delimiters) > > >>> "'" > "'" > >>> '"' > '"' > >>> """'"'""" > '\'"\'' > >>> '''"'"''' > '"\'"' > >>> > > (Note that the interactive console displays results using repr(), and hence > escapes ' that are internal to avoid conflict with the ones wrapping the > output) > > >>> repr('''"'"''') > '\'"\\\'"\'' > >>> str('''"'"''') > '"\'"' > >>> print('''"'"''') > "'" > >>> > > The print() operation does not wrap the output with extraneous quotes. Thank your insightful explanation. Regards, HZ > -- > Wulfraed Dennis Lee Bieber AF6VN > wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list