Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert an Integer List to a Float List in Python

#1
How to Convert an Integer List to a Float List in Python

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[0]))
# <class 'int'>

Let’s apply the built-in function float, 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[0]))
# <class ‘int’>

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[0]))
#<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.

The post How to Convert an Integer List to a Float List in Python first appeared on Finxter.



https://www.sickgaming.net/blog/2020/12/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,395 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,051 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 2,107 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,956 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,556 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,697 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,608 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,512 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,534 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String into List of Substrings xSicKxBot 0 1,441 12-11-2022, 12:17 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016