The most pythonic way to convert a list of integers fs to a list of floats is to use the one-line
fs = [float(x) for x in fs]. It iterates over all the elements in the list
fs using list comprehension and converts each element of the list x to an integer value using the float (x) constructor.
This article shows you the simplest ways to convert a one-dimensional list consisting only of int to a list of float.
Problem: Given a list of ints
[1, 2, 3]. How to convert it to a list of floats
[1.0, 2.0, 3.0]?
The methods are not applicable to lists of lists, they contain rounding errors that are different in each method. If necessary, you can add cycles or define custom functions.
Method 1: List Comprehension
Suppose we have a list:
a = [1, 3, 2, 1, 5, -2]
Now, check the type of the list numbers:
print(type(a)) # <class 'int'>
Let’s apply the built-in function floa
t, and get a list of floats:
print([float(i) for i in a]) # [1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
Check the type of numbers in the new list:
A = [float(i) for i in a] print(type(A)) # <class ‘float’>
Thus, using the built-in float function, we can get a new list of floats in one line of code.
Method 2: Map Function
The built-in function
map is well optimized and efficient, when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.
Apply to the same list
a the following code:
print(list(map(float, a))) # [1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
We will not check the type of the elements of the resulting list, because when calling the ‘map’ function, we passed the ‘float’ function already described in method 1 as an argument, and wrapped the result in a list using the ‘list’ function.
Method 3: Enumerate Function
Using the built-in function ‘enumerate’, we can loop through the elements of the list and process not only the value of the element, but also its index number in the list:
for i, item in enumerate(a): a[i] = float(item) #[1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
Method 4: NumPy
Here’s a look at converting a list from an int to an array using the NumPy module. The difference between an array and a list is that all elements of an array must be of the same type, like “float” and “int”. Numeric operations with large amounts of data can be performed with arrays much faster and more efficiently than with lists.
Let’s turn our first list a into an array:
import numpy as np N = np.array(a, float) #[1., 3., 2., 1., 5., -2.]
We pass two arguments to the array function, the name of the list to convert to an array and the type for each element.
Сheck the type of elements:
print(type(N)) #<class 'numpy.float64'>
Unlike the ‘float’ type of numbers in Python, the numpy module defines them slightly differently and is divided into several subgroups. For example, ‘float64’ is a numpy numeric data type used to store double precision real numbers, in which 1 bit is assigned to the sign, 11 bits for the exponent and 52 for the mantissa, ‘float32’ contains 1 bit for the sign, 8 bits for exponent and 23 for mantissa, ‘float16’ – 1 bit for the sign, 5 bits for exponent and 10 for mantissa. This must be taken into account when calculating with arrays.