美文网首页
python:使用列表中元素的一个属性进行排序

python:使用列表中元素的一个属性进行排序

作者: 树里的熊 | 来源:发表于2022-10-14 21:39 被阅读0次

场景描述:

open =[ node1,node2.....]
node:

    def __init__(self, parent, g):
        self.parent = parent
        self.g = g
        self.f = self.evaluate_heuristic() + self.g
        self.state = self._get_state()

需要实现:Pop the best node in open(Use the attribute node.f in comparison)

解决办法:

open.sort(key=lambda node: node.f, reverse=False)
cur = open[0]
open.remove(cur)

(当然也可以直接用queue)
reverse=False:升序
reverse=True:降序

相关文章

网友评论

      本文标题:python:使用列表中元素的一个属性进行排序

      本文链接:https://www.haomeiwen.com/subject/pwskzrtx.html