Given the following coordinates: (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Sort the coordinates in Python (in ascending order) by comparing the second part of the coordinates. You may use the sort() function in Python.
Python code:
# Sort the coordinates in Python (in ascending order) by
comparing the second part of the coordinates.
# Using the sort() function in Python.
def last(n):
return n[m]
# function to sort
def sort(tuples):
return sorted(tuples, key = last)
# driver code
coordinates = [('z', 9), ('z', 2), ('t', 4), ('x', 8), ('b', 1),
('m', 7)]
m = 1 #key position
print("Sorted:"),
print(sort(coordinates))
Output:
Get Answers For Free
Most questions answered within 1 hours.