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



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 1,134 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 918 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 866 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 837 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 669 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 779 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 641 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 659 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 728 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String into List of Substrings xSicKxBot 0 638 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