Reputation: 1202
I have dataset which have mixed values, in which few values are float(e.g. 3.4) and few are int (e.g. 3.0). I want to check if all the values are double. I want that if the values are either int or float, they are accepted as double datatype.
I tried doing:
data = [3.0, 3.4, 3.2, 3.0, 3.1]
for valuedata in data:
is_double = isinstance(valuedata, float)
print(is_double)
The result is coming out to be FALSE, where as i want that int and float both should be accepted as double.
Thanks
Upvotes: 2
Views: 480
Reputation:
You can see if it's an instance of numbers.Number.
>>> import numbers
>>> isinstance(1.1, numbers.Number)
True
>>> isinstance(1, numbers.Number)
True
>>>
Upvotes: 2
Reputation: 3064
You can check for multiple allowed types by passing a tuple to isinstance()
:
is_double = isinstance(valuedata, (int, float))
Also note that Python's int
has unlimited precision, which could be too large to fit into a C/C++ double
.
Upvotes: 0