-1

I am trying to implement the A* algorithm in Python as a learning exercise. I have a list of nodes, called open, that contains strings that represent the names of graph nodes. Example: open["node_a", "node_b", "node_c"]. I also have two defaultdict dictionaries, f_estim and g, that contain values like {"name_of_node" : integer_representing_a_certain_cost_associated_to_the_node}. I am required to sort the open list ascending by the value associated in f_estim, and if that value is equal for two nodes, descending according to the value in g. I am sorting ascending like so: open.sort(key=lambda node: f_estim[node]). How can i also sort descending when reaching two equal values? I could not find the answer in the documentation of the sort function. Thank you!

M_S
  • 25
  • 1
  • 3

1 Answers1

0

You could do something like this:

open.sort(key=lambda node: (f_estim[node], -g[node]))
Osama A.Rehman
  • 912
  • 1
  • 6
  • 12
  • Note that this will only work for ordering criteria that are numeric, and not for other types of comparable criteria (e.g. strings) – newacct Sep 13 '20 at 03:51