Python: return float 1.0 as int 1 but float 1.5 as float 1.5 [duplicate]

跟風遠走 提交于 2020-12-29 08:54:55

问题


In Python is there a way to turn 1.0 into a integer 1 while the same function ignores 1.5 and leaves it as a float?

Right now, int() will turn 1.0 into 1 but it will also round 1.5 down to 1, which is not what I want.


回答1:


Continuing from the comments above:

Using is_integer():

Example from the docs:

>>> (1.5).is_integer()
False
>>> (1.0).is_integer()
True
>>> (1.4142135623730951).is_integer()
False
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False

INPUT:

s = [1.5, 1.0, 2.5, 3.54, 1.0, 4.4, 2.0]

Hence:

print([int(x) if x.is_integer() else x for x in s])

Wrapped in a function:

def func(s):
    return [int(x) if x.is_integer() else x for x in s]

print(func(s))

If you do not want any import:

def func(s):
    return [int(x) if x == int(x) else x for x in s]

print(func(s))

Using map() with lambda function and the iter s:

print(list(map(lambda x: int(x) if x.is_integer() else x, s)))

OR

print(list(map(lambda x: int(x) if int(x) == x else x, s)))

OUTPUT:

[1.5, 1, 2.5, 3.54, 1, 4.4, 2]



回答2:


In case your goal is to convert numbers to a concise string, you could simply use '%g' ("General Format") for formatting:

>>> '%g' % 1.0
'1'
>>> '%g' % 1
'1'
>>> '%g' % 1.5
'1.5'
>>> '%g' % 0.3
'0.3'
>>> '%g' % 0.9999999999
'1'

You can specify the desired accuracy:

>>> '%.15g' % 0.999999999999999
'0.999999999999999'
>>> '%.2g' % 0.999
'1'



回答3:


float.is_integer is a method on floats that returns whether or not the float represents an integer.

You can just use this function I made called to_int, that uses is_integer to check whether it represents an integer (e.g. 1.0) or not (e.g. 1.5).

If it represents an integer, return int(a), otherwise just return it's original value.

As you see, I am not using elif or else because return returns only once:

def to_int(a):
   if a.is_integer():
      return int(a)
   return a

print(to_int(1.5))
print(to_int(1.0))

Output:

1.5
1



回答4:


Python floats are approximations, so something that prints as 1.0 is not necessarily exactly 1.0. If you want to see if something is approximately an integer, use a sufficiently small epsilon value.

EPSILON = 0.0001 # Make this smaller or larger depending on desired accuracy

def func(x):
  if abs(x - round(x)) < EPSILON:
    return round(x)
  else:
    return x

In general, if you're checking whether a float is == to something, that tends to be a code smell, as floating point values are inherently approximate. It's more appropriate in general to check whether a float is near something, within some epsilon range.




回答5:


for list of numbers:

def get_int_if_possible(list_of_numbers):
    return [int(x) if x == int(x) else x for x in list_of_numbers]

for one number:

def get_int_if_possible(number):
    return int(number) if number == int(number) else number



回答6:


A simple thing you could do is use the modulo operator:

if (myFloat % 1 == 0) // Number is an int
else // numer is not an int

EDIT: Python code

if myFloat % 1 == 0:
  # myFloat is an integer.
else:
  # myFloat is NOT an integer



回答7:


What I used to do in the past in C++ is, lets say you have these variables:

float x = 1.5;
float y = 1.0;

Then you could do something like this:

if(x == (int)x) 
    return 1;
else return 0;

This will return 0 because 1.5 is not equal to 1

if(y == (int)y) 
    return 1;
else return 0;

This will return 1 because 1.0 is equal to 1

Of course your question is about Python and the function is_integer() should work great, I just thought some people might find this useful.




回答8:


def your_function(i):
  if i.is_integer():
    return int(i)
  else:
    return float(i)



回答9:


divmod alternative:

def f(x):
    return x if divmod(x, 1)[1] else int(x)


来源:https://stackoverflow.com/questions/55510485/python-return-float-1-0-as-int-1-but-float-1-5-as-float-1-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!