5 Best Ways to Convert Array of Floats to Integers in Python

πŸ’‘ Problem Formulation: You are working with an array of floating-point numbers in Python, and you need to convert each element to an integer. This could occur in data preparation processes, where real numbers need to be converted into discrete values. Consider an input example like [1.7, 2.3, 3.9], where the desired output would be an array of integers like [1, 2, 3] or [2, 2, 4] depending on the rounding method applied.

Method 1: Using the int() Function and List Comprehension

This method utilizes the built-in int() function in combination with list comprehension to truncate each float to an integer. The int() function effectively removes the decimal portion of the float, resulting in its integer part.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

floats = [1.7, 2.3, 3.9]
integers = [int(num) for num in floats]

Output:

[1, 2, 3]

The code snippet creates a new list of integers from ‘floats’ by iterating over each element, converting it to an integer, and including it in the ‘integers’ list. The decimal parts are simply discarded.

Method 2: Using the map() Function with int

Here, map() is used to apply the int() function to each element in the float array. This is a functional programming approach to convert all elements of the array to integers.

Here’s an example:

floats = [1.7, 2.3, 3.9]
integers = list(map(int, floats))

Output:

[1, 2, 3]

This code maps the int() function over the ‘floats’ list, effectively casting each float to an integer and discarding the decimal part. The resulting map object is then converted back to a list.

Method 3: Using NumPy’s astype() Method

When working with large datasets, NumPy’s astype() method allows efficient conversion of an array’s data-type. It is particularly useful for arrays stored as NumPy ndarrays.

Here’s an example:

import numpy as np
floats_np = np.array([1.7, 2.3, 3.9])
integers_np = floats_np.astype(int)

Output:

[1 2 3]

By using NumPy’s astype(int), every element of the ‘floats_np’ array is converted to an integer. The decimals are truncated, and an array of integers is obtained.

Method 4: Using math.floor() or math.ceil()

The math.floor() function rounds down to the nearest integer, while math.ceil() rounds up. These can be applied to each element in the array according to the desired rounding direction.

Here’s an example:

import math
floats = [1.7, 2.3, 3.9]
floor_integers = [math.floor(num) for num in floats]
ceil_integers = [math.ceil(num) for num in floats]

Output:

# Flooring
[1, 2, 3]
# Ceiling
[2, 3, 4]

This snippet demonstrates two list comprehensions, one using math.floor() and the other using math.ceil(), that process the ‘floats’ list and produce two new lists with the values rounded down or up, respectively.

Bonus One-Liner Method 5: Using Round Function

The built-in round() function can be used for rounding floats to the nearest integer. If no second argument is given, it rounds to the nearest integer using “round half to even” strategy.

Here’s an example:

floats = [1.7, 2.3, 3.9]
rounded_integers = [round(num) for num in floats]

Output:

[2, 2, 4]

With this one-liner, the round() function is applied to each element in the array ‘floats’ via a list comprehension, resulting in a new list with integers where each float is rounded to the nearest integer following standard rounding rules.

Summary/Discussion

  • Method 1: Using int() and List Comprehension. Strengths: Easy to understand and implement. Weaknesses: Does not provide options for different rounding strategies; it simply truncates the floats.
  • Method 2: Using map() and int(). Strengths: Compact and functional style. Weaknesses: Less intuitive for those unfamiliar with functional programming paradigms.
  • Method 3: Using NumPy’s astype(). Strengths: Offers good performance with large arrays. Weaknesses: Requires NumPy, which may be an unnecessary dependency for simple tasks or small arrays.
  • Method 4: Using math.floor() or math.ceil(). Strengths: Provides control over the rounding direction. Weaknesses: Requires importing the math module; slightly more verbose.
  • Method 5: Using Round Function. Strengths: Simple and handles rounding using the standard round half to even strategy. Weaknesses: May not be as precise as floor or ceil when a specific rounding direction is required.