Open In App
Related Articles

Python | Convert location coordinates to tuple

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate working of
# Convert location coordinates to tuple
# Using tuple() + float() + split() + map()
 
# Initializing string
test_str = "44.6463, -49.583"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Convert location coordinates to tuple
# Using tuple() + float() + split() + map()
res = tuple(map(float, test_str.split(', ')))
 
# printing result
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

# Python3 code to demonstrate working of
# Convert location coordinates to tuple
# Using eval()
 
# Initializing string
test_str = "44.6463, -49.583"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Convert location coordinates to tuple
# Using eval()
res = eval(test_str)
 
# printing result
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

# Initializing string
test_str = "44.6463, -49.583"
 
# Using a generator expression to convert the coordinates to a tuple
coordinates = tuple(float(coord) for coord in test_str.split(', '))
 
# Printing the result
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:

  1. Import the numpy library.
  2. Initialize a string variable test_str with a string value.
  3. Print the original string.
  4. Use the numpy fromstring() method to convert the string to a numpy array.
  5. Split the string on the comma delimiter, and pass the resulting list to the fromstring() method.
  6. Set the dtype parameter of the fromstring() method to float to ensure that the array values are treated as floating-point numbers.
  7. Print the resulting numpy array.

Python3

import numpy as np
 
# input string
test_str = "44.6463, -49.583"
 
# convert string to 1D numpy array of float values
arr = np.fromstring(test_str, sep=", ", dtype=float)
 
# reshape array to a 2D array of shape (1, 2)
res = arr.reshape((1, 2))
 
# print result
print("The coordinates after conversion to tuple are:", res)
#This code is contributed  by Pushpa.

                    
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.



Last Updated : 15 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads