Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon May 16 20:48:02 EDT 2022


On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy... at gmail.com"
<hongyi.zhao at 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("'"))

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.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list