RE: Convert '165.0' to int

2011-08-01 Thread Prasad, Ramit
-Original Message- From: python-list-bounces+ramit.prasad=jpmchase@python.org [mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of Gregory Ewing Sent: Sunday, July 24, 2011 7:05 PM To: python-list@python.org Subject: Re: Convert '165.0' to int Fra

RE: Convert '165.0' to int

2011-08-01 Thread Prasad, Ramit
-Original Message- From: python-list-bounces+ramit.prasad=jpmchase@python.org [mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of Frank Millman Sent: Monday, July 25, 2011 12:51 AM To: python-list@python.org Subject: Re: Convert '165.0' to int On

Re: Convert '165.0' to int

2011-07-25 Thread Billy Mays
On 07/25/2011 05:48 AM, Steven D'Aprano wrote: But if you're calling a function in both cases: map(int, data) [int(x) for x in data] I am aware the premature optimization is a danger, but its also incorrect to ignore potential performance pitfalls. I would favor a generator expression here

Re: Convert '165.0' to int

2011-07-25 Thread SigmundV
On Jul 25, 10:48 am, Steven D'Aprano wrote: > > One other important proviso: if your map function is a wrapper around a > Python expression: > > map(lambda x: x+1, data) > [x+1 for x in data] > > then the list comp will be much faster, due to the overhead of the function > call. List comps and gen

Re: Convert '165.0' to int

2011-07-25 Thread Steven D'Aprano
On Mon, 25 Jul 2011 10:07 am Billy Mays wrote: > On 7/24/2011 2:27 PM, SigmundV wrote: >> list_of_integers = map(string_to_int, list_of_strings) >> >> Of course, this will be horribly slow if you have thousands of >> strings. In such a case you should use an iterator (assuming you use >> python 2

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 25, 2:04 am, Gregory Ewing wrote: > Frank Millman wrote: > > I know I am flogging a dead horse here, but IMHO, '165', '165.', > > '165.0', and '165.00' are all valid string representations of the > > integer 165.[1] > > > Therefore, for practical purposes, it would not be wrong for python's

Re: Convert '165.0' to int

2011-07-24 Thread Chris Angelico
On Mon, Jul 25, 2011 at 10:07 AM, Billy Mays wrote: > if the goal is speed, then you should use generator expressions: > > list_of_integers = (int(float(s)) for s in list_of_strings) Clarification: This is faster if and only if you don't actually need it as a list. In spite of the variable name,

Re: Convert '165.0' to int

2011-07-24 Thread Billy Mays
On 7/24/2011 2:27 PM, SigmundV wrote: On Jul 21, 10:31 am, "Frank Millman" wrote: Is there a short cut, or must I do this every time (I have lots of them!) ? I know I can write a function to do this, but is there anything built-in? I'd say that we have established that there is no shortcut, n

Re: Convert '165.0' to int

2011-07-24 Thread Gregory Ewing
Frank Millman wrote: I know I am flogging a dead horse here, but IMHO, '165', '165.', '165.0', and '165.00' are all valid string representations of the integer 165.[1] Therefore, for practical purposes, it would not be wrong for python's 'int' function to accept these without complaining. How

Re: Convert '165.0' to int

2011-07-24 Thread SigmundV
On Jul 21, 10:31 am, "Frank Millman" wrote: > Is there a short cut, or must I do this every time (I have lots of them!) ? > I know I can write a function to do this, but is there anything built-in? I'd say that we have established that there is no shortcut, no built- in for this. You write you ow

Re: Convert '165.0' to int

2011-07-24 Thread Chris Angelico
On Sun, Jul 24, 2011 at 6:53 PM, Ben Finney wrote: > Frank Millman writes: > >> I know I am flogging a dead horse here, but IMHO, '165', '165.', >> '165.0', and '165.00' are all valid string representations of the >> integer 165.[1] > > I disagree entirely. Once you introduce a decimal point into

Re: Convert '165.0' to int

2011-07-24 Thread Ben Finney
Frank Millman writes: > On Jul 24, 10:53 am, Ben Finney wrote: > > Frank Millman writes: > > > I know I am flogging a dead horse here, but IMHO, '165', '165.', > > > '165.0', and '165.00' are all valid string representations of the > > > integer 165.[1] > > > > I disagree entirely. Once you int

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 24, 10:53 am, Ben Finney wrote: > Frank Millman writes: > > I know I am flogging a dead horse here, but IMHO, '165', '165.', > > '165.0', and '165.00' are all valid string representations of the > > integer 165.[1] > > I disagree entirely. Once you introduce a decimal point into the > repr

Re: Convert '165.0' to int

2011-07-24 Thread Ben Finney
Frank Millman writes: > I know I am flogging a dead horse here, but IMHO, '165', '165.', > '165.0', and '165.00' are all valid string representations of the > integer 165.[1] I disagree entirely. Once you introduce a decimal point into the representation, you're no longer representing an integer

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 23, 8:28 pm, rantingrick wrote: > On Jul 23, 1:53 am, Frank Millman wrote: > > >-- > > The ideal solution is the one I sketched out earlier - modify python's > > 'int' function to accept strings such as '165.0'. > >---

Re: Convert '165.0' to int

2011-07-24 Thread Chris Angelico
On Sun, Jul 24, 2011 at 6:21 PM, Frank Millman wrote: > On Jul 24, 10:07 am, Chris Angelico wrote: >> if dec.rtrim('0')!='': >> >> ChrisA > > I think you meant 'rstrip', but yes, neater and faster. > > Thanks Yeah, I did. Mea culpa... every language has it somewhere, but I keep mucking up which

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 24, 10:07 am, Chris Angelico wrote: > On Sun, Jul 24, 2011 at 5:58 PM, Frank Millman wrote: > >  if int(dec) != 0: > > to > >    if [_ for _ in list(dec) if _ != '0']: > > if dec.rtrim('0')!='': > > ChrisA I think you meant 'rstrip', but yes, neater and faster. Thanks Frank -- http://m

Re: Convert '165.0' to int

2011-07-24 Thread Chris Angelico
On Sun, Jul 24, 2011 at 5:58 PM, Frank Millman wrote: >  if int(dec) != 0: > to >    if [_ for _ in list(dec) if _ != '0']: > if dec.rtrim('0')!='': ChrisA -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert '165.0' to int

2011-07-24 Thread Thomas 'PointedEars' Lahn
Billy Mays wrote: > On 7/21/2011 10:40 PM, Thomas 'PointedEars' Lahn wrote: >> Billy Mays wrote: >>> On 07/21/2011 08:46 AM, Web Dreamer wrote: If you do not want to use 'float()' try: int(x.split('.')[0]) >>> >>> This is right. >> >> Assuming that the value of `x' is in the proper

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 24, 9:34 am, Steven D'Aprano wrote: > Frank Millman wrote: > > If I really wanted to be 100% safe, how about this - > > >     def get_int(s): > >         if '.' in s: > >             num, dec = s.split('.', 1) > >             if dec != '': > >                 if int(dec) != 0: > >          

Re: Convert '165.0' to int

2011-07-24 Thread Ben Finney
Steven D'Aprano writes: > As a toy for learning about regexes, that's fine, but I trust you would > never use that in production. There are less verbose ways of wasting time > and memory. +1 QotW -- \ “I may disagree with what you say, but I will defend to the | `\death your

Re: Convert '165.0' to int

2011-07-24 Thread Steven D'Aprano
Frank Millman wrote: > If I really wanted to be 100% safe, how about this - > > def get_int(s): > if '.' in s: > num, dec = s.split('.', 1) > if dec != '': > if int(dec) != 0: > raise ValueError('Invalid literal for int') >

Re: Convert '165.0' to int

2011-07-24 Thread Steven D'Aprano
Billy Mays wrote: > I'll probably get flak for this, but damn the torpedoes: > > def my_int(num): > import re > try: > m = re.match('^(-?[0-9]+)(.0)?$', num) > return int(m.group(1)) As a toy for learning about regexes, that's fine, but I trust you would never use tha

Re: Convert '165.0' to int

2011-07-24 Thread Frank Millman
On Jul 23, 5:12 pm, Billy Mays wrote: > On 7/23/2011 3:42 AM, Chris Angelico wrote: > > > > > int(s.rstrip('0').rstrip('.')) > > Also, it will (in?)correct parse strings such as: > > '16500' > > to 165. > > -- > Bill True enough. If I really wanted to be 100% safe, how ab

Re: Convert '165.0' to int

2011-07-23 Thread Dan Stromberg
On Sat, Jul 23, 2011 at 8:53 PM, Billy Mays wrote: > I'll probably get flak for this, but damn the torpedoes: > > def my_int(num): >import re >try: >m = re.match('^(-?[0-9]+)(.0)?$', num) >return int(m.group(1)) >except AttributeError: >#raise your own error, o

Re: Convert '165.0' to int

2011-07-23 Thread Billy Mays
On 7/23/2011 2:28 PM, rantingrick wrote: On Jul 23, 1:53 am, Frank Millman wrote: -- The problem with that is that it will silently ignore any non-zero digits after the point. Of course int(float(x)) does the same, which I had overlooked. ---

Re: Convert '165.0' to int

2011-07-23 Thread rantingrick
On Jul 23, 1:53 am, Frank Millman wrote: >-- > The problem with that is that it will silently ignore any non-zero > digits after the point. Of course int(float(x)) does the same, which I > had overlooked. >---

Re: Convert '165.0' to int

2011-07-23 Thread Chris Angelico
On Sun, Jul 24, 2011 at 1:12 AM, Billy Mays wrote: > On 7/23/2011 3:42 AM, Chris Angelico wrote: >> >> int(s.rstrip('0').rstrip('.')) >> > > Also, it will (in?)correct parse strings such as: > > '16500' > > to 165. Yes, it will, but is that an issue to the OP? Programming

Re: Convert '165.0' to int

2011-07-23 Thread Billy Mays
On 7/23/2011 3:42 AM, Chris Angelico wrote: int(s.rstrip('0').rstrip('.')) Also, it will (in?)correct parse strings such as: '16500' to 165. -- Bill -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert '165.0' to int

2011-07-23 Thread Frank Millman
On Jul 23, 10:23 am, Steven D'Aprano wrote: > Frank Millman wrote: > > To recap, the original problem is that it would appear that some third- > > party systems, when serialising int's into a string format, add a .0 > > to the end of the string. I am trying to get back to the original int > > safe

Re: Convert '165.0' to int

2011-07-23 Thread Frank Millman
On Jul 23, 9:42 am, Chris Angelico wrote: > On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman wrote: > > The problem with that is that it will silently ignore any non-zero > > digits after the point. Of course int(float(x)) does the same, which I > > had overlooked. > > If you know that there will a

Re: Convert '165.0' to int

2011-07-23 Thread Steven D'Aprano
Frank Millman wrote: > To recap, the original problem is that it would appear that some third- > party systems, when serialising int's into a string format, add a .0 > to the end of the string. I am trying to get back to the original int > safely. > > The ideal solution is the one I sketched out

Re: Convert '165.0' to int

2011-07-23 Thread Chris Angelico
On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman wrote: > The problem with that is that it will silently ignore any non-zero > digits after the point. Of course int(float(x)) does the same, which I > had overlooked. If you know that there will always be a trailing point, you can trim off any traili

Re: Convert '165.0' to int

2011-07-22 Thread Frank Millman
On Jul 22, 9:59 pm, Terry Reedy wrote: > On 7/22/2011 1:55 AM, Frank Millman wrote: > > > As the OP, I will clarify what *my* requirement is. This discussion > > has gone off at various tangents beyond what I was asking for. > > Typical. Don't worry about it ;-). > > > As suggested above, I am onl

Re: Convert '165.0' to int

2011-07-22 Thread Chris Angelico
On Sat, Jul 23, 2011 at 5:32 AM, rantingrick wrote: > That's nine-quadrillion people! Only for galactic measurements or > microscopic reasons would you need such large numbers. > Never decide that "nobody would need numbers bigger than X". Someone will. One common thing to do with big numbers is

Re: Convert '165.0' to int

2011-07-22 Thread Terry Reedy
On 7/22/2011 1:55 AM, Frank Millman wrote: As the OP, I will clarify what *my* requirement is. This discussion has gone off at various tangents beyond what I was asking for. Typical. Don't worry about it ;-). As suggested above, I am only talking about a string containing int literals follow

Re: Convert '165.0' to int

2011-07-22 Thread rantingrick
On Jul 22, 2:32 pm, rantingrick wrote: > >>> '{0:,.0f}'.format(2**53) > '9,007,199,254,740,992' Would have been better to say >>> '{0:,}'.format(2**53) '9,007,199,254,740,992' -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert '165.0' to int

2011-07-22 Thread rantingrick
On Jul 22, 7:42 am, Hrvoje Niksic wrote: > Frank Millman writes: > > int(float(x)) does the job, and I am happy with that. I was just > > asking if there were any alternatives. > > int(float(s)) will corrupt integers larger than 2**53, should you ever > need them.  int(decimal.Decimal(s)) works w

Re: Convert '165.0' to int

2011-07-22 Thread Grant Edwards
On 2011-07-22, Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote: > On 07/22/2011 10:58 AM, Grant Edwards wrote: >> On 2011-07-22, Billy >> Mays<81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote: >>> Properly formatted means that Python would accep

Re: Convert '165.0' to int

2011-07-22 Thread Billy Mays
On 07/22/2011 10:58 AM, Grant Edwards wrote: On 2011-07-22, Billy Mays<81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote: Properly formatted means that Python would accept the string as an argument to float() without raising an exception. Then you can't assume that '.' is t

Re: Convert '165.0' to int

2011-07-22 Thread Grant Edwards
On 2011-07-22, Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote: > On 07/22/2011 10:21 AM, Grant Edwards wrote: >> While that may be clear to you, that's because you've made some >> assumptions. "Convert a properly formatted string representation of a >> floating po

Re: Convert '165.0' to int

2011-07-22 Thread Billy Mays
On 07/22/2011 10:21 AM, Grant Edwards wrote: While that may be clear to you, that's because you've made some assumptions. "Convert a properly formatted string representation of a floating point number to an integer" is not a rigorous definition. What does "properly formatted" mean? Who says t

Re: Convert '165.0' to int

2011-07-22 Thread Grant Edwards
On 2011-07-22, Billy Mays wrote: > On 7/21/2011 10:40 PM, Thomas 'PointedEars' Lahn wrote: >> Billy Mays wrote: >> >>> On 07/21/2011 08:46 AM, Web Dreamer wrote: If you do not want to use 'float()' try: int(x.split('.')[0]) >>> >>> This is right. >> >> Assuming that the value of `x'

Re: Convert '165.0' to int

2011-07-22 Thread Hrvoje Niksic
Frank Millman writes: > int(float(x)) does the job, and I am happy with that. I was just > asking if there were any alternatives. int(float(s)) will corrupt integers larger than 2**53, should you ever need them. int(decimal.Decimal(s)) works with numbers of arbitrary size. -- http://mail.pytho

Re: Convert '165.0' to int

2011-07-21 Thread Frank Millman
On Jul 21, 10:00 pm, Terry Reedy wrote: > On 7/21/2011 10:13 AM, Grant Edwards wrote: > > > On 2011-07-21, Web Dreamer  wrote: > >> Leo Jay a ?crit ce jeudi 21 juillet 2011 11:47 dans > > >> int(x.split('.')[0]) > > >> But, the problem is the same as with int(float(x)), the integer number is > >>

Re: Convert '165.0' to int

2011-07-21 Thread Billy Mays
On 7/21/2011 10:40 PM, Thomas 'PointedEars' Lahn wrote: Billy Mays wrote: On 07/21/2011 08:46 AM, Web Dreamer wrote: If you do not want to use 'float()' try: int(x.split('.')[0]) This is right. Assuming that the value of `x' is in the proper format, of course. Else you might easily cut t

Re: Convert '165.0' to int

2011-07-21 Thread Thomas 'PointedEars' Lahn
Billy Mays wrote: > On 07/21/2011 08:46 AM, Web Dreamer wrote: >> If you do not want to use 'float()' try: >> >> int(x.split('.')[0]) > > This is right. Assuming that the value of `x' is in the proper format, of course. Else you might easily cut to the first one to three digits of a string rep

Re: Convert '165.0' to int

2011-07-21 Thread Terry Reedy
On 7/21/2011 10:13 AM, Grant Edwards wrote: On 2011-07-21, Web Dreamer wrote: Leo Jay a ?crit ce jeudi 21 juillet 2011 11:47 dans int(x.split('.')[0]) But, the problem is the same as with int(float(x)), the integer number is still not as close as possible as the original float value. Nobo

Re: Convert '165.0' to int

2011-07-21 Thread Grant Edwards
On 2011-07-21, Web Dreamer wrote: > Leo Jay a ?crit ce jeudi 21 juillet 2011 11:47 dans > int(x.split('.')[0]) > > But, the problem is the same as with int(float(x)), the integer number is > still not as close as possible as the original float value. Nobody said that "close as possible to the o

Re: Convert '165.0' to int

2011-07-21 Thread Billy Mays
On 07/21/2011 08:46 AM, Web Dreamer wrote: If you do not want to use 'float()' try: int(x.split('.')[0]) This is right. But, the problem is the same as with int(float(x)), the integer number is still not as close as possible as the original float value. I would in fact consider doing this:

Re: Convert '165.0' to int

2011-07-21 Thread Frank Millman
> > [1] See separate thread on apparent inconsisteny in timeit timings.- Hide > quoted text - > I must have done something wrong - it is consistent now. Here are the results - C:\Python32\Lib>timeit.py "int(float('165.0'))" 10 loops, best of 3: 3.51 usec per loop C:\Python32\Lib>timeit.py

Re: Convert '165.0' to int

2011-07-21 Thread Frank Millman
On Jul 21, 11:53 am, Thomas Jollans wrote: > On 21/07/11 11:31, Frank Millman wrote: > > > Hi all > > > I want to convert '165.0' to an integer. > > Well, it's not an integer. What does your data look like? How do you > wish to convert it to int? Do they all represent decimal numbers? If so, > how

Re: Convert '165.0' to int

2011-07-21 Thread Frank Millman
On Jul 21, 11:47 am, Leo Jay wrote: > On Thu, Jul 21, 2011 at 5:31 PM, Frank Millman wrote: > > > Hi all > > > I want to convert '165.0' to an integer. > > > The obvious method does not work - > > x = '165.0' > int(x) > > > Traceback (most recent call last): > >  File "", line 1, in >

Re: Convert '165.0' to int

2011-07-21 Thread Thomas Jollans
On 21/07/11 11:31, Frank Millman wrote: > Hi all > > I want to convert '165.0' to an integer. Well, it's not an integer. What does your data look like? How do you wish to convert it to int? Do they all represent decimal numbers? If so, how do you want to round them? What if you get '165.xyz' as i

Re: Convert '165.0' to int

2011-07-21 Thread Leo Jay
On Thu, Jul 21, 2011 at 5:31 PM, Frank Millman wrote: > > Hi all > > I want to convert '165.0' to an integer. > > The obvious method does not work - > x = '165.0' int(x) > > Traceback (most recent call last): >  File "", line 1, in > ValueError: invalid literal for int() with base 10: '

Convert '165.0' to int

2011-07-21 Thread Frank Millman
Hi all I want to convert '165.0' to an integer. The obvious method does not work - x = '165.0' int(x) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '165.0' If I convert to a float first, it does work - int(float(x)) 165 Is