Re: best way to remove leading zeros from a tuple like string
On Sun, May 20, 2018 at 12:54 PM,wrote: > Lets say I have the following tuple like string. > (128, 020, 008, 255) > > What is the best way to to remove leading zeroes and end up with the > following. > (128, 20, 8, 255)-- I do not care about spaces > > This is the solution I came up with > s = "(128, 020, 008, 255)" > v = s.replace ("(", "") > v = v.replace (")", "") > vv = ",".join([str(int(i)) for i in v.split(",")]) > final = "(" + vv + ")" I actually like your solution pretty well - especially the splitting on "," and applying int() to the pieces. If you want a simple regex solution though, the following should work on Python 1.5 through 3.7b3: $ pythons --command 'import re; print("(" + re.sub("(\(| )0*", "", "(128, 020, 008, 255)"))' below cmd output started 2018 Wed May 23 01:23:00 PM PDT /usr/local/cpython-1.0/bin/python (1.0.1) bad Traceback (innermost last): File "", line 1 ImportError: No module named re /usr/local/cpython-1.1/bin/python (1.1) bad Traceback (innermost last): File "", line 1, in ? ImportError: No module named re /usr/local/cpython-1.2/bin/python (1.2) bad Traceback (innermost last): File "", line 1, in ? ImportError: No module named re /usr/local/cpython-1.3/bin/python (1.3) bad Traceback (innermost last): File "", line 1, in ? ImportError: No module named re /usr/local/cpython-1.4/bin/python (1.4) bad Traceback (innermost last): File "", line 1, in ? ImportError: No module named re /usr/local/cpython-1.5/bin/python (1.5.2) good (128,20,8,255) /usr/local/cpython-1.6/bin/python (1.6.1) good (128,20,8,255) /usr/local/cpython-2.0/bin/python (2.0.1) good (128,20,8,255) /usr/local/cpython-2.1/bin/python (2.1.0) good (128,20,8,255) /usr/local/cpython-2.2/bin/python (2.2.0) good (128,20,8,255) /usr/local/cpython-2.3/bin/python (2.3.0) good (128,20,8,255) /usr/local/cpython-2.4/bin/python (2.4.0) good (128,20,8,255) /usr/local/cpython-2.5/bin/python (2.5.6) good (128,20,8,255) /usr/local/cpython-2.6/bin/python (2.6.9) good (128,20,8,255) /usr/local/cpython-2.7/bin/python (2.7.13) good (128,20,8,255) /usr/local/cpython-3.0/bin/python (3.0.1) good (128,20,8,255) /usr/local/cpython-3.1/bin/python (3.1.5) good (128,20,8,255) /usr/local/cpython-3.2/bin/python (3.2.5) good (128,20,8,255) /usr/local/cpython-3.3/bin/python (3.3.3) good (128,20,8,255) /usr/local/cpython-3.4/bin/python (3.4.2) good (128,20,8,255) /usr/local/cpython-3.5/bin/python (3.5.0) good (128,20,8,255) /usr/local/cpython-3.6/bin/python (3.6.0) good (128,20,8,255) /usr/local/cpython-3.7/bin/python (3.7.0b3) good (128,20,8,255) /usr/local/jython-2.7/bin/jython (2.7.0) good (128,20,8,255) /usr/local/pypy-5.10.0/bin/pypy (2.7.13) good (128,20,8,255) /usr/local/pypy-5.3.1/bin/pypy (2.7.10) good (128,20,8,255) /usr/local/pypy-5.9.0/bin/pypy (2.7.13) good (128,20,8,255) /usr/local/pypy-6.0.0/bin/pypy (2.7.13) good (128,20,8,255) /usr/local/pypy3-5.10.0/bin/pypy3 (3.5.3) good (128,20,8,255) /usr/local/pypy3-5.5.0/bin/pypy3 (3.3.5) good (128,20,8,255) /usr/local/pypy3-5.8.0-with-lzma-fixes/bin/pypy3 (3.5.3) good (128,20,8,255) /usr/local/pypy3-5.8.0/bin/pypy3 (3.5.3) good (128,20,8,255) /usr/local/pypy3-5.9.0/bin/pypy3 (3.5.3) good (128,20,8,255) /usr/local/pypy3-6.0.0/bin/pypy3 (3.5.3) good (128,20,8,255) /usr/local/micropython-git-2017-06-16/bin/micropython (3.4.0) good (128,20,8,255) HTH -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Wed, May 23, 2018 at 12:50 AM, Grant Edwardswrote: > On 2018-05-22, Thomas Jollans wrote: >> On 2018-05-20 23:54, Paul wrote: >>> you will find several useful sites where you can test regexes. Regex >>> errors are very common, even after you have experience with them. >> >> What's the benefit of those compared to simply trying out the regex in a >> Python console? > > Doesn't everybody have an executable file in their home directory > named "testit.py" which is continually morphed to test different > Python features? > Certainly not! Mine is ~/tmp/runme.py in case I need other files with it. :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On 2018-05-22, Thomas Jollanswrote: > On 2018-05-20 23:54, Paul wrote: >> you will find several useful sites where you can test regexes. Regex >> errors are very common, even after you have experience with them. > > What's the benefit of those compared to simply trying out the regex in a > Python console? Doesn't everybody have an executable file in their home directory named "testit.py" which is continually morphed to test different Python features? -- Grant Edwards grant.b.edwardsYow! What's the MATTER at Sid? ... Is your BEVERAGE gmail.comunsatisfactory? -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
Thomas Jollans wrote: > On 2018-05-20 23:54, Paul wrote: > > you will find several useful sites where you can test regexes. > > What's the benefit of those compared to simply trying out the regex in a > Python console? > Possibly nothing. But there are obvious benefits compared to trying to write and test such an expression in one's head, which was the situation which led me to suggest it. :) > -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On 2018-05-20 23:54, Paul wrote: > you will find several useful sites where you can test regexes. Regex > errors are very common, even after you have experience with them. What's the benefit of those compared to simply trying out the regex in a Python console? -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
Am 21.05.2018 um 01:16 schrieb bruceg113...@gmail.com: If I decide I need the parentheses, this works. "(" + ",".join([str(int(i)) for i in s[1:-1].split(",")]) + ")" '(128,20,8,255,-1203,1,0,-123)' Thanks, Bruce Creating the tuple seems to be even simpler. >>> str(tuple(map(int, s[1:-1].split("," '(128, 20, 8, 255, -1203, 1, 0, -123)' Wolfram -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Monday, May 21, 2018 at 1:05:52 PM UTC-4, Rodrigo Bistolfi wrote: > >>> repr(tuple(int(i) for i in s[1:-1].split(','))) > '(128, 20, 8, 255, -1203, 1, 0, -123)' > > 2018-05-21 4:26 GMT-03:00 Peter Otten <__pete...@web.de>: > > > bruceg113...@gmail.com wrote: > > > > > Looking over the responses, I modified my original code as follows: > > > > > s = "(128, 020, 008, 255, -1203,01,-000, -0123)" > > ",".join([str(int(i)) for i in s[1:-1].split(",")]) > > > '128,20,8,255,-1203,1,0,-123' > > > > I think this looks better with a generator instead of the listcomp: > > > > >>> ",".join(str(int(i)) for i in s[1:-1].split(",")) > > '128,20,8,255,-1203,1,0,-123' > > > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > I am not familiar with the repr statement. Looking into your response, I like it. There is no need to add-in the parentheses. With the repr statment: >>> bb = repr (tuple (int (i) for i in s [1: -1] .split (','))) >>> bb '(128, 20, 8, 255, -1203, 1, 0, -123)' >>> type(bb) >>> Without the repr statement: >>> aa = (tuple (int (i) for i in s [1: -1] .split (','))) >>> aa (128, 20, 8, 255, -1203, 1, 0, -123) >>> type(aa) >>> Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
>>> repr(tuple(int(i) for i in s[1:-1].split(','))) '(128, 20, 8, 255, -1203, 1, 0, -123)' 2018-05-21 4:26 GMT-03:00 Peter Otten <__pete...@web.de>: > bruceg113...@gmail.com wrote: > > > Looking over the responses, I modified my original code as follows: > > > s = "(128, 020, 008, 255, -1203,01,-000, -0123)" > ",".join([str(int(i)) for i in s[1:-1].split(",")]) > > '128,20,8,255,-1203,1,0,-123' > > I think this looks better with a generator instead of the listcomp: > > >>> ",".join(str(int(i)) for i in s[1:-1].split(",")) > '128,20,8,255,-1203,1,0,-123' > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
bruceg113...@gmail.com wrote: > Looking over the responses, I modified my original code as follows: > s = "(128, 020, 008, 255, -1203,01,-000, -0123)" ",".join([str(int(i)) for i in s[1:-1].split(",")]) > '128,20,8,255,-1203,1,0,-123' I think this looks better with a generator instead of the listcomp: >>> ",".join(str(int(i)) for i in s[1:-1].split(",")) '128,20,8,255,-1203,1,0,-123' -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Sunday, May 20, 2018 at 6:56:05 PM UTC-4, Ben Bacarisse wrote: > "Michael F. Stemper"writes: > > > On 2018-05-20 16:19, Ben Bacarisse wrote: > >> bruceg113...@gmail.com writes: > >> > >>> Lets say I have the following tuple like string. > >>>(128, 020, 008, 255) > >>> > >>> What is the best way to to remove leading zeroes and end up with the > >>> following. > >>>(128, 20, 8, 255)-- I do not care about spaces > >> > >> You could use a regexp: > >> > >>import re > >>... > >>re.sub(r"(? >> > >> I post this because I think it works (interesting corner cases are 10005 > >> and 000), > > > > Seeing this makes me realize that mine will eliminate any numbers that > > are all leading zero, including '0'. Also, forms like '-0042' will be > > left unchanged. > > I realised after posting the negatives won't work. Not, I suspect, an > issue for the OP but -0042 can certainly be said to have "leading > zeros". > > > Maybe splitting it into integer forms and sending each through > > str( int( ) ) would be the safest. > > Yup. I gave a version of that method too which handles negative numbers > by accident (by leaving the - in place!). A better version would be > > re.sub(r"-?[0-9]+", lambda m: str(int(m.group(0))), s) > > > -- > Ben. Looking over the responses, I modified my original code as follows: >>> s = "(128, 020, 008, 255, -1203,01,-000, -0123)" >>> ",".join([str(int(i)) for i in s[1:-1].split(",")]) '128,20,8,255,-1203,1,0,-123' If I decide I need the parentheses, this works. >>> "(" + ",".join([str(int(i)) for i in s[1:-1].split(",")]) + ")" '(128,20,8,255,-1203,1,0,-123)' Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
"Michael F. Stemper"writes: > On 2018-05-20 16:19, Ben Bacarisse wrote: >> bruceg113...@gmail.com writes: >> >>> Lets say I have the following tuple like string. >>>(128, 020, 008, 255) >>> >>> What is the best way to to remove leading zeroes and end up with the >>> following. >>>(128, 20, 8, 255)-- I do not care about spaces >> >> You could use a regexp: >> >>import re >>... >>re.sub(r"(?> >> I post this because I think it works (interesting corner cases are 10005 >> and 000), > > Seeing this makes me realize that mine will eliminate any numbers that > are all leading zero, including '0'. Also, forms like '-0042' will be > left unchanged. I realised after posting the negatives won't work. Not, I suspect, an issue for the OP but -0042 can certainly be said to have "leading zeros". > Maybe splitting it into integer forms and sending each through > str( int( ) ) would be the safest. Yup. I gave a version of that method too which handles negative numbers by accident (by leaving the - in place!). A better version would be re.sub(r"-?[0-9]+", lambda m: str(int(m.group(0))), s) -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On 2018-05-20 16:19, Ben Bacarisse wrote:bruceg113...@gmail.com writes:Lets say I have the following tuple like string. (128, 020, 008, 255) What is the best way to to remove leading zeroes and end up with the following. (128, 20, 8, 255)-- I do not care about spacesYou could use a regexp: import re ... re.sub(r"(?and 000),Seeing this makes me realize that mine will eliminate any numbers that are all leading zero, including '0'. Also, forms like '-0042' will be left unchanged. Maybe splitting it into integer forms and sending each through str( int( ) ) would be the safest. This partly does the trick, but packing the results back into a string that resembles a tuple has been left as a short exercise for the student: >>> tuple = '(0128, 020, 000, 008, -0042,255)' >>> fields = tuple[1:len(tuple)-1].split(",") >>> for field in fields: ... print( str(int(field)) ) ... 128 20 0 8 -42 255 >>> -- Michael F. Stemper Why doesn't anybody care about apathy? -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
bruceg113...@gmail.com wrote: > Lets say I have the following tuple like string. > (128, 020, 008, 255) > > What is the best way to to remove leading zeroes and end up with the > following. > (128, 20, 8, 255)-- I do not care about spaces > > This is the solution I came up with > s = "(128, 020, 008, 255)" > v = s.replace ("(", "") > v = v.replace (")", "") > vv = ",".join([str(int(i)) for i in v.split(",")]) > final = "(" + vv + ")" Two more: >>> s = "(128, 020, 008, 255, -1203,01,-000, -0123)" >>> str(tuple(map(int, s[1:-1].split("," '(128, 20, 8, 255, -1203, 1, 0, -123)' >>> re.sub(r"\b0+\B", "", s) '(128, 20, 8, 255, -1203,1,-0, -123)' -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Sunday, May 20, 2018 at 5:32:32 PM UTC-4, Paul wrote: > > > > > > This works for me: mytuplestring.replace("0","") > > > > Your regex will also eliminate non-leading zeros. Your right, what was I thinking? -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Sun, May 20, 2018, 5:53 PM Paulwrote: > This works for me: mytuplestring.replace("0","") > >> >>> Your regex will also eliminate non-leading zeros. >> >> > If you Google > > regex tester > > you will find several useful sites where you can test regexes. Regex > errors are very common, even after you have experience with them. > -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
> > > This works for me: mytuplestring.replace("0","") > > Your regex will also eliminate non-leading zeros. -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On Sunday, May 20, 2018 at 5:01:08 PM UTC-4, Michael F. Stemper wrote: > On 2018-05-20 14:54, bruceg113...@gmail.com wrote: > > Lets say I have the following tuple like string. > >(128, 020, 008, 255) > > > > What is the best way to to remove leading zeroes and end up with the > > following. > >(128, 20, 8, 255)-- I do not care about spaces > > > I'd use a few regular expressions: > > >>> from re import sub > >>> tuple = '(0128, 020, 008,012, 255)' > >>> sub( " 0*", " ", tuple ) # leading zeroes following space(s) > '(0128, 20, 8,012, 255)' > >>> sub( ",0*", ",", tuple ) # leading zeroes following comma > '(0128, 020, 008,12, 255)' > >>> sub( "\(0*", "(", tuple ) # leading zeroes after opening parend > '(128, 020, 008,012, 255)' > > Each step could be written as "tuple = sub( ..." > > >>> tuple = sub( " 0*", " ", tuple ) # following space(s) > >>> tuple = sub( ",0*", ",", tuple ) # following comma > >>> tuple = sub( "\(0*", "(", tuple ) # after opening parend > >>> tuple > '(128, 20, 8,12, 255)' > >>> > > > Or, if you like to make your code hard to read and maintain, you could > combine them all into a single expression: > > >>> sub( " 0*", " ", sub( ",0*", ",", sub( "\(0*", "(", tuple ) ) ) > '(128, 20, 8,12, 255)' > >>> > > -- > Michael F. Stemper > What happens if you play John Cage's "4'33" at a slower tempo? I did not think about using regular expressions. After your response, I looked into regular expressions and also found Regex.Replace After thinking about my question, I said why not use a replace statement. This works for me: mytuplestring.replace("0","") Thanks for a good starting point:) Bruce -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
bruceg113...@gmail.com writes: > Lets say I have the following tuple like string. > (128, 020, 008, 255) > > What is the best way to to remove leading zeroes and end up with the > following. > (128, 20, 8, 255)-- I do not care about spaces You could use a regexp: import re ... re.sub(r"(?https://mail.python.org/mailman/listinfo/python-list
Re: best way to remove leading zeros from a tuple like string
On 2018-05-20 14:54, bruceg113...@gmail.com wrote: Lets say I have the following tuple like string. (128, 020, 008, 255) What is the best way to to remove leading zeroes and end up with the following. (128, 20, 8, 255)-- I do not care about spaces I'd use a few regular expressions: >>> from re import sub >>> tuple = '(0128, 020, 008,012, 255)' >>> sub( " 0*", " ", tuple ) # leading zeroes following space(s) '(0128, 20, 8,012, 255)' >>> sub( ",0*", ",", tuple ) # leading zeroes following comma '(0128, 020, 008,12, 255)' >>> sub( "\(0*", "(", tuple ) # leading zeroes after opening parend '(128, 020, 008,012, 255)' Each step could be written as "tuple = sub( ..." >>> tuple = sub( " 0*", " ", tuple ) # following space(s) >>> tuple = sub( ",0*", ",", tuple ) # following comma >>> tuple = sub( "\(0*", "(", tuple ) # after opening parend >>> tuple '(128, 20, 8,12, 255)' >>> Or, if you like to make your code hard to read and maintain, you could combine them all into a single expression: >>> sub( " 0*", " ", sub( ",0*", ",", sub( "\(0*", "(", tuple ) ) ) '(128, 20, 8,12, 255)' >>> -- Michael F. Stemper What happens if you play John Cage's "4'33" at a slower tempo? -- https://mail.python.org/mailman/listinfo/python-list
best way to remove leading zeros from a tuple like string
Lets say I have the following tuple like string. (128, 020, 008, 255) What is the best way to to remove leading zeroes and end up with the following. (128, 20, 8, 255)-- I do not care about spaces This is the solution I came up with s = "(128, 020, 008, 255)" v = s.replace ("(", "") v = v.replace (")", "") vv = ",".join([str(int(i)) for i in v.split(",")]) final = "(" + vv + ")" Thanks, Bruce -- https://mail.python.org/mailman/listinfo/python-list