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!
你可以这样做:
open.sort(key=lambda node: (f_estim[node], -g[node]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.