-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
-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
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
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
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
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
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,
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
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
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
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
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
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
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
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'.
> >---
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
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
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
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
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:
> >
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
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')
>
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
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
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
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.
---
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.
>---
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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'
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
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
> >>
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
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
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
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
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:
>
> [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
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
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
>
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
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: '
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
56 matches
Mail list logo