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

<div><p class="has-pale-cyan-blue-background-color has-background">The most pythonic way to convert a list of integers fs to a list of floats is to use the one-line <code>fs = [float(x) for x in fs]</code>. It iterates over all the elements in the list <code>fs</code> using list comprehension and converts each element of the list x to an integer value using the float (x) constructor.</p>
<p>This article shows you the simplest ways to <strong>convert a one-dimensional <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">list </a>consisting only of int to a list of float</strong>.</p>
<p><strong>Problem</strong>: Given a list of ints <code>[1, 2, 3]</code>. How to convert it to a list of floats <code>[1.0, 2.0, 3.0]</code>?</p>
<p>The methods are not applicable to <a href="https://blog.finxter.com/python-list-of-lists/" target="_blank" rel="noreferrer noopener">lists of lists</a>, they contain rounding errors that are different in each method. If necessary, you can add cycles or define custom functions.</p>
<h2>Method 1: List Comprehension</h2>
<p>Suppose we have a list:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">a = [1, 3, 2, 1, 5, -2]
</pre>
<p>Now, check the type of the list numbers:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(type(a[0]))
# &lt;class 'int'>
</pre>
<p>Let’s apply the built-in function floa<em><code>t</code></em>, and get a list of floats:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([float(i) for i in a])
# [1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
</pre>
<p>Check the type of numbers in the new list:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">A = [float(i) for i in a]
print(type(A[0]))
# &lt;class ‘int’>
</pre>
<p>Thus, using the built-in float function, we can get a new list of floats in one line of code.</p>
<h2>Method 2: Map Function</h2>
<p>The built-in function <code><a href="https://blog.finxter.com/daily-python-puzzle-string-encrpytion-ord-function-map-function/" title="Mastering the Python Map Function [+Video]" target="_blank" rel="noreferrer noopener">map</a></code> 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.</p>
<p>Apply to the same list <code>a</code> the following code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(list(map(float, a)))
# [1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
</pre>
<p>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.</p>
<h2>Method 3: Enumerate Function</h2>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i, item in enumerate(a): a[i] = float(item)
#[1.0, 3.0, 2.0, 1.0, 5.0, -2.0]
</pre>
<h2>Method 4: NumPy</h2>
<p>Here’s a look at converting a list from an int to an array using the <a href="https://blog.finxter.com/numpy-tutorial/">NumPy </a>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.</p>
<p>Let’s turn our first list a into an array:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
N = np.array(a, float)
#[1., 3., 2., 1., 5., -2.]
</pre>
<p>We pass two arguments to the array function, the name of the list to convert to an array and the type for each element.</p>
<p>Сheck the type of elements: </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(type(N[0]))
#&lt;class 'numpy.float64'>
</pre>
<p>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.</p>
<p>The post <a href="https://blog.finxter.com/how-to-convert-an-integer-list-to-a-float-list-in-python/" target="_blank" rel="noopener noreferrer">How to Convert an Integer List to a Float List in Python</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


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



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016