Sometimes in a competitive coding environment, we get input in some other datatypes and we need to convert them into other forms this problem is the same as when we have an input in the form of string and we need to convert it into floats. Let’s discuss a few ways to convert an array of strings in Python to an array of floats.
Input: ['1.1' '1.5' '2.7' '8.9'] Output: [ 1.1 1.5 2.7 8.9] Explanation: In This, we are converting an array of strings to an array of floats.
Convert Array of Strings to Array of Float in Python
We can convert Python String Array to Float using different methods.
- Using for loop and float() function
- Using List Comprehension
- Using map() function
- Using astype
- Using Numpy
- Using np.fromstring
- Using np.asarray()
- Using np.asfarray
Convert String Array to Array Float using for loop and float() function
Here is the approach to convert an array of strings from Python to an array float using loop and float functions. Create an empty array to store the converted floats. Loop through each element of the input array and convert the current element to a float using the float() function. Append the converted float to the output array. Return the output array. Below is the implementation of the above approach.
Python3
def convert_strings_to_floats(input_array): output_array = [] for element in input_array: converted_float = float (element) output_array.append(converted_float) return output_array input_array = [ '1.1' , '1.5' , '2.7' , '8.9' ] output_array = convert_strings_to_floats(input_array) print (output_array) |
Output
[1.1, 1.5, 2.7, 8.9]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n), since we are creating a new array to store the converted floats.
Convert String Array to Array Float using List Comprehension
Here is the approach to convert an array of strings from Python to an array float using list comprehension. List comprehension provides a concise way to convert an array of strings to an array of floats.
Python3
string_array = [ "5.55" , "6.66" , "7.77" ] float_array = [ float (string) for string in string_array] print (float_array) |
Output
[5.55, 6.66, 7.77]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)
Convert String Array to Array Float using Map
Here is the approach to convert an array of strings from Python to an array float using the map() function. We have used the map() function to apply the float() function to each element in the input list. It Returns the resulting iterator as a list using the list() function. Here is the implementation of this approach.
Python3
def convert_to_floats(arr): # Use the map function to apply the float function to each element in the input list result = map ( float , arr) # Return the resulting iterator as a list return list (result) # Test the function arr = [ '1.1' , '1.5' , '2.7' , '8.9' ] print (convert_to_floats(arr)) |
Output
[1.1, 1.5, 2.7, 8.9]
Time complexity: O(n)
Auxiliary Space: O(n)
Python String to float using Astype
Here is the approach to convert an array of strings from Python to an array float using astype(). It is used to change the datatype of a series. if a column could be imported as a string but to do operations we have to convert it into a float, astype() is used to do such data type conversions.
Python3
import numpy as np ini_array = np.array([ "1.1" , "1.5" , "2.7" , "8.9" ]) # printing initial array print ( "initial array" , str (ini_array)) # converting to array of floats # using np.astype res = ini_array.astype(np. float ) print ( "final array" , str (res)) |
Output
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1 1.5 2.7 8.9]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)
Convert an Array of Strings to an Array of Floats in Numpy
Convert Python String Array to Array Float using np.fromstring
Here is the approach to convert the array of strings from Python to an array float using np.fromstring(). The Numpy.fromstring() function creates a new one-dimensional array initialized from text data in a string.
Python3
import numpy as np ini_array = np.array([ "1.1" , "1.5" , "2.7" , "8.9" ]) # printing initial array print ( "initial array" , str (ini_array)) # converting to array of floats # using np.fromstring ini_array = ', ' .join(ini_array) ini_array = np.fromstring(ini_array, dtype = np. float , sep = ', ' ) print ( "final array" , str (ini_array)) |
Output
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1 1.5 2.7 8.9]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)
Convert Python String Array to Array Float using np.asarray()
Here is the approach to convert an array of strings from Python to an array float using np.asarray(). The numpy.asarray()function is used when we want to convert the input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays.
Python3
import numpy as np ini_array = np.array([ "1.1" , "1.5" , "2.7" , "8.9" ]) print ( "initial array" , str (ini_array)) # converting to array of floats # using np.asarray final_array = b = np.asarray(ini_array, dtype = np.float64, order = 'C' ) print ( "final array" , str (final_array)) |
Output
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1 1.5 2.7 8.9]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)
Convert Python String Array to Array Float using np.asfarray
Here is the approach to convert an array of strings from Python to array float using np.asfarray(). The numpy.asfarray() function is used when we want to convert the input to a float-type array. Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and arrays.
Python3
import numpy as np ini_array = np.array([ "1.1" , "1.5" , "2.7" , "8.9" ]) # printing initial array print ( "initial array" , str (ini_array)) # converting to array of floats # using np.asarray final_array = b = np.asfarray(ini_array,dtype = float ) print ( "final array" , str (final_array)) |
Output
initial array ['1.1' '1.5' '2.7' '8.9']
final array [1.1 1.5 2.7 8.9]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)