Python | Convert location coordinates to tuple
Last Updated :
15 May, 2023
Sometimes, while working with locations, we need a lot of data which has location points in form of latitudes and longitudes. These can be in form of a string and we desire to get tuple versions of same. Let’s discuss certain ways in which this task can be performed.
Method #1 :
Using tuple() + float() + split() + map() The combination of above functions can be used to perform this task. In this, we first split the two parts of coordinates into a list, apply float function to each of them using float() and map() and lastly it is converted to tuple using tuple().
Python3
test_str = " 44.6463 , - 49.583 "
print ("The original string is : " + str (test_str))
res = tuple ( map ( float , test_str.split( ', ' )))
print ("The coordinates after conversion to tuple are : " + str (res))
|
Output :
The original string is : 44.6463, -49.583
The coordinates after conversion to tuple are : (44.6463, -49.583)
Method #2 :
Using eval() This is the one-liner and recommended method to perform this particular task. In this, the eval(), internally detects the string and converts to floating point number separated as tuple elements.
Python3
test_str = " 44.6463 , - 49.583 "
print ("The original string is : " + str (test_str))
res = eval (test_str)
print ("The coordinates after conversion to tuple are : " + str (res))
|
Output :
The original string is : 44.6463, -49.583
The coordinates after conversion to tuple are : (44.6463, -49.583)
Method #3: Using regular expression
Approach
using regular expressions to match the latitude and longitude values in the original string. We then convert the matched strings to floats and create a tuple with the two values.
Algorithm
1. Import the re module.
2. Use a regular expression to match the latitude and longitude values in the original string.
3. Convert the matched latitude and longitude strings to floats.
4. Create a tuple with the latitude and longitude floats.
5. Return the tuple.
Python3
import re
def convert_coordinates(coordinates_str):
lat_str, long_str = re.findall(r '[-+]?\d*\.\d+|\d+' , coordinates_str)
lat_float = float (lat_str)
long_float = float (long_str)
return (lat_float, long_float)
coordinates_str = "44.6463, -49.583"
print (convert_coordinates(coordinates_str))
|
Output
(44.6463, -49.583)
Time complexity: O(1)
Auxiliary Space: O(1)
Method 4 : using a generator expression:
step-by-step approach
Initialize a string variable test_str with the value “44.6463, -49.583”.
Use the split() method with ‘, ‘ as the delimiter to split the test_str string into a list of two string values, “44.6463” and “-49.583”.
Use a generator expression to loop through the list of string values and convert each string value to a float.
Use the tuple() constructor to create a tuple of the two floating-point numbers.
Assign the tuple of floating-point numbers to the variable coordinates.
Print the message “The coordinates after conversion to tuple are :” along with the value of the coordinates variable.
Python3
test_str = "44.6463, -49.583"
coordinates = tuple ( float (coord) for coord in test_str.split( ', ' ))
print ( "The coordinates after conversion to tuple are :" , coordinates)
|
Output
The coordinates after conversion to tuple are : (44.6463, -49.583)
Time complexity of O(n), where n is the length of the input string.
The auxiliary space complexity is O(n), as it creates a tuple of n floating-point numbers.
Method 5: using numpy:
- Import the numpy library.
- Initialize a string variable test_str with a string value.
- Print the original string.
- Use the numpy fromstring() method to convert the string to a numpy array.
- Split the string on the comma delimiter, and pass the resulting list to the fromstring() method.
- Set the dtype parameter of the fromstring() method to float to ensure that the array values are treated as floating-point numbers.
- Print the resulting numpy array.
Python3
import numpy as np
test_str = "44.6463, -49.583"
arr = np.fromstring(test_str, sep = ", " , dtype = float )
res = arr.reshape(( 1 , 2 ))
print ( "The coordinates after conversion to tuple are:" , res)
|
Output:
The coordinates after conversion to tuple are: [[ 44.6463 -49.583 ]]
Time complexity: O(n), where n is the length of the string being converted to a numpy array.
Space complexity: O(n), where n is the length of the string being converted to a numpy array. This is because the resulting numpy array will take up space in memory proportional to the length of the string.
Share your thoughts in the comments
Please Login to comment...