Sometimes, while working with Python data, we can have a problem in which we need to convert a float number into a list of digits. This problem is common in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using builtin methods
Python3
# Python3 code to demonstrate working of # Convert Float to digit list # initialize N N = 6.456 # printing N print ( "The floating number is : " + str (N)) # Convert float to digit list x = str (N) res = [] digs = "0123456789" for i in x: if i in digs: res.append( int (i)) # Printing result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time Complexity: O(n), where n is the number of digits in the float number N.
Auxiliary Space: O(n), where n is the number of digits in the float number N, as the res list is used to store the digits of N.
Method #2: Using list comprehension + isdigit():
The combination of the above functions can be used to perform this task. In this, we first convert the float number to string and then iterate it converting each digit to an integer and constructing the list using list comprehension.
Python3
# Python3 code to demonstrate working of # Convert Float to digit list # using list comprehension + isdigit() # initialize N N = 6.456 # printing N print ( "The floating number is : " + str (N)) # Convert Float to digit list # using list comprehension + isdigit() res = [ int (ele) for ele in str (N) if ele.isdigit()] # printing result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time Complexity: O(n), where n is the length of the string representation of the floating-point number N.
Auxiliary Space: O(n), where n is the length of the string representation of the floating-point number N.
Method #3: Using map() + regex expression + findall():
The combination of above functionalities can be used to perform this task. In this, we iterate through list using map() and extract and convert each element of float number using regex expression and findall().
Python3
# Python3 code to demonstrate working of # Convert Float to digit list # using map() + regex expression + findall() import re # initialize N N = 6.456 # printing N print ( "The floating number is : " + str (N)) # Convert Float to digit list # using map() + regex expression + findall() res = list ( map ( int , re.findall( '\d' , str (N)))) # printing result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time complexity: O(log N), where N is the input floating-point number.
Auxiliary space: O(log N), where N is the input floating-point number.
Method #4: Using str(),replace(),list(),map() methods
Approach:
- Initialize a float value N.
- Print the original float value.
- Convert the float value to a string using str() function.
- Remove the decimal point from the string using replace() function.
- Convert the string into a list of integers using map() and int() functions.
- Store the list of integers in a variable.
- Print the list of integers as the result.
Example:
Python3
# Python3 code to demonstrate working of # Convert Float to digit list # initialize N N = 6.456 # Printing N print ( "The floating number is : " + str (N)) # Converting float to digit list x = str (N).replace( "." , "") res = list ( map ( int , x)) # Printing the result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time Complexity: O(n) where n is the number of elements in the list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list.
Method #5: Using str(),split(),map() methods
Python3
#Python3 code to demonstrate working of #Convert Float to digit list #initialize N N = 6.456 #printing N print ( "The floating number is : " + str (N)) #Convert Float to digit list x = str (N).split( "." ) res = list ( map ( int , x[ 0 ] + x[ 1 ])) #printing result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time complexity: O(n) as they need to iterate through the string/float number.
Auxiliary Space: O(n) as it needs to store the list of digits.
Method 6: Using string manipulation
Python3
# initialize N N = 6.456 # printing N print ( "The floating number is : " + str (N)) # Convert Float to digit list using string manipulation num_str = str (N) res = [] for c in num_str: if c.isdigit(): res.append( int (c)) # printing result print ( "List of floating numbers is : " + str (res)) |
The floating number is : 6.456 List of floating numbers is : [6, 4, 5, 6]
Time complexity: O(N) where N is the length of the string representation of the float.
Auxiliary space: O(N) since we are storing the digits in the ‘res’ list, which can have at most N elements.