![]() |
|
[Tut] How to Convert a Float List to an Integer List in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert a Float List to an Integer List in Python (/thread-98213.html) |
[Tut] How to Convert a Float List to an Integer List in Python - xSicKxBot - 11-11-2020 How to Convert a Float List to an Integer List in Python The most Pythonic way to convert a list of floats Code: fsCode: fs = [int(x) for x in fs]Code: fsCode: xCode: int(x)This article shows you the simplest ways to convert a one-dimensional listconsisting only of floats to a list of int. Problem: Given a list of floats Code: [1.0, 2.0, 3.0]Code: [1, 2, 3]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 to check, account for and minimize errors. Method 1: List Comprehension Suppose we have a list: Code: a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]Now, check the type of the list numbers: Code: print(type(a[0]))
# <class 'float'>Let’s apply the built-in function Code: intCode: print([int(a) for a in a])
# [1, 1, 1, 0, 5, -2]Check the type of numbers in the new list: Code: A = [int(a) for a in a]
print(type(A[0]))
# <class ‘int’>Thus, using the built-in function Code: intMethod 2: Map Function The built-in function Code: mapApply to the same list Code: aCode: print(list(map(int, a)))
# [1, 1, 1, 0, 5, -2]It makes no sense to check the type of the elements of the resulting list, since when we called the Code: mapCode: intCode: listThe quality of this transformation of the list, or rather the rounding error, is the same as in the first method. Method 3: Round & List Comprehension It is very similar to the first, but unlike Code: intCode: print([round(a) for a in a])Check the type of numbers in the new list: Code: D = [round(a) for a in a]
print(type(D[0]))
# <class ‘int’>As you can see from this example, there are different built-in functions to achieve our goal, the difference is in the method and the magnitude of the rounding error. Method 4: Math Module In this way, I suggest using the imported module Code: mathCode: ceil()Code: floor()Code: trunc()Let’s apply to the original list: Code: a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]
print([math.ceil(a) for a in a])
# [2, 2, 2, 1, 6, -2]‘Ceil’ rounds to the next largest integer, respecting the sign(-2.3 < -2 which is True). Check the type of numbers in the new list: Code: C = [math.ceil(a) for a in a]
print(type(C[0]))
# <class ‘int’>Consider the following function in the ‘math’ – ‘floor’ module, which is the opposite of ‘ceil’ – rounding down to the nearest integer: Code: print([math.floor(a) for a in a])
# [1, 1, 1, 0, 5, -3]Check the type: Code: F = [math.floor(a) for a in a]
print(type(F[0]))
# <class ‘int’>The next function, Code: trunc()Code: int()Code: print([math.trunc(a) for a in a])
# [1, 1, 1, 0, 5, -2]And check the type: Code: T = [math.trunc(a) for a in a]
print(type(T[0]))
# <class ‘int’>Here’s a look at converting a list from an Code: intLet’s turn our first list Code: aCode: import numpy as np
N = np.array(a, int)We pass two arguments to the array function, the name of the list to convert to an array and the type for each element. Code: # [ 1 1 1 0 5 -2]Сheck the type of elements: Code: print(type(N[0]))
# <class 'numpy.int32'>Unlike the Code: intCode: 'int32'Code: 'int64'Code: 'int'The post How to Convert a Float List to an Integer List in Python first appeared on Finxter. |