![](/img/trans.png)
[英]How can i sort the matplotlib graph in ascending or descending order?
[英]How can i use the sort function if i want to sort ascending by a criterion and descending by another criterion?
我正在尝试在 Python 中实现 A* 算法作为学习练习。 我有一个名为open
的节点列表,其中包含表示图形节点名称的字符串。 示例: open["node_a", "node_b", "node_c"]
。 我还有两个defaultdict
字典f_estim
和g
,它们包含{"name_of_node" : integer_representing_a_certain_cost_associated_to_the_node}
。 我需要按f_estim
关联的值对open
列表进行升序排序,如果两个节点的值相等,则根据g
的值降序。 我按升序排序: open.sort(key=lambda node: f_estim[node])
。 当达到两个相等的值时,我如何还降序排序? 我在sort
函数的文档中找不到答案。 谢谢!
你可以这样做:
open.sort(key=lambda node: (f_estim[node], -g[node]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.