Reputation:
Is there a code to define / sort through lists?
Upvotes: 0
Views: 128
Reputation: 515
A quick and straightforward solution would be to create a list of results and then corroborate the index with your list of remaining points (because they are inherently lined up). Below is a step-by-step process to achieving this, and at the very bottom is a cleaned-up version of the code.
def find_closest(start_point, remaining_points):
results = [] # list of results for later use
for element in list(remaining_points):
result = distance(start_point, element)
results.append(result) # append the result to the list
# After iteration is finished, find the lowest result
lowest_result = min(results)
# Find the index of the lowest result
lowest_result_index = results.index(lowest_result)
# Corroborate this with the inputs
closest_point = remaining_points[lowest_result_index]
# Return the closest point
return closest_point
Or to simplify the code:
def find_closest(start_point, remaining_points):
results = []
for element in remaining_points:
results.append(distance(start_point, element))
return remaining_points[results.index(min(results))]
Edit: you commented saying you can't use Python's in-built min()
function. A solution would be to just create your own functional minimum_value()
function.
def minimum_value(lst: list):
min_val = lst[0]
for item in lst:
if item < min_val:
min_val = item
return min_val
def find_closest(start_point, remaining_points):
results = []
for element in remaining_points:
results.append(distance(start_point, element))
return remaining_points[results.index(minimum_value(results))]
Upvotes: 0
Reputation: 1506
You can use the min
function of python with the key
argument like this:
def find_closest(start_point, remaining_points):
return min(remaining_points, key=lambda a: distance(start_point, a))
Because of your specific needs (only loops and if statements), here is another solution. For other people who are not restricted, I recommend using the above solution.
def find_closest(start_point, remaining_points):
closest = None
closest_distance = 1000000
# Better but more advanced initialisation
# closest_distance = float("inf")
for element in list(remaining_points):
dist = distance(start_point, element)
if dist < closest_distance:
closest = element
closest_distance = dist
return closest
Before going through all points, we initialise the closest
point to None
(it is not found yet) and the closest_distante
to a very high value (to be sure that the first evaluated point will be closer).
Then, for each point in remaining_points
, we calculate its distance from start_point
and store it in dist
.
If this distance dist
is less than closest_distance
, then the current point is closest from the current stored one, so we update the stored closest point closest
with the current point and we update the closest_distance
with the current distance dist
.
When all points have been evaluated, we return the closest point closest
.
min
function: https://www.programiz.com/python-programming/methods/built-in/minlambda
function: https://www.w3schools.com/python/python_lambda.aspUpvotes: 6