![]() |
VOOZH | about |
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.
We can convert Python String Array to Float using different methods.
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.
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.
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.
Output
[5.55, 6.66, 7.77]
Time Complexity: O(n), where n is the length of the input array.
Auxiliary Space: O(n)
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.
Output
[1.1, 1.5, 2.7, 8.9]
Time complexity: O(n)
Auxiliary Space: O(n)
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.
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)
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.
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)
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.
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)
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.
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)